Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Good Evening All.,

I am doing hotel management project in windows forms.
In that when customer vacate the room. i have to take maximum checkoutid and increased to one and update.
This logic is kept in checkout button.

my project have multiple users.
so one problem is there
i.e. two or more user checked checkout button same time all user having same maximum no. and increased by 1 and updated.
so multiple rooms having same checkoutno.
i don't known how to avoid this.
checkoutno column is unique.
Posted
Updated 18-May-11 1:12am
v2

The issue is called concurrency, and is a huge issue for any system architect. Basically there are two courses that you can take. Pessimistic Concurrency and Optimistic Concurrency.

1. Pessimistic concurrency is where you actively put a lock on any object that is being updated. In your case, say one user opened the check out form, you would put a lock on the checkoutid column in your database, and no other user could access the check out till the first user was through and released the lock. The advantage of this is that it is easy to implement and you also know that no data can be overwritten unintentionally. The disadvantage of this is that if you have more than a few users, this system does not scale well, and if your user opens up the form, then goes to lunch, that table could be locked till they get back.

2. Optimistic concurrency is where you take into account that you will get 2 or more users updating the record at the same time, and you put into place a system whereby you check to see if the data has been changed since it was last retrieved, and you abort the transaction if it is true. In your case, you could maybe put a timestamp column in the table to be updated, and this column will be updated to the current time whenever the checkoutid is updated. When your user accesses the check out form, you retrieve the timestamp and keep it in memory. Then you would check the current timestamp each time you want to update checkoutid, and if it does not match your in memory timestamp then abort the transaction, or create a new checkoutid with the current data etc.

Hope this helps
 
Share this answer
 
Comments
yesotaso 18-May-11 8:10am    
I used pessimistic approach before, ease of implementation vs user problems... It was quite a messy match. I had to implement 5-6 lock clean mechanism for each lock mechanism:) User PC crashes, power-shortages (they didnt connect switches to UPS) causing unforseen network problems, and some more stuff like arrogant users with habit of using task manager to close apps...
Wayne Gaylard 18-May-11 8:32am    
Yea - it's only recommended for small apps with only a few users. Best approach is to plan for things to happen, and take action accordingly.
ajitha.pusapati 18-May-11 8:17am    
ok..
which way is better..
i am fresher i dont know any thing..
ajitha.pusapati 18-May-11 8:19am    
can u give the direction..
Wayne Gaylard 18-May-11 8:30am    
Just Google for Concurrency C# application and you will get a lot of results. This is a huge topic, and only you the developer can know which is best for your particular application, everyone else can only point you in the right direction.
Hope this[^] might help you.
 
Share this answer
 
Comments
ajitha.pusapati 18-May-11 7:56am    
i cont understand this and i dont know how to impliment
ajitha.pusapati 18-May-11 7:57am    
This is my code

int maxcoid = Convert.ToInt32(CoreManager.DC.CheckIns.Max(a => a.CheckOutId));
if (maxcoid != null)
chkIn.CheckOutId = maxcoid + 1;
else
chkIn.CheckOutId = 1;
ajitha.pusapati 18-May-11 8:00am    
I think in web application Application.lock()
But in windows forms...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900