This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JDBC and the fly likes Simultaneous Update of same record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Simultaneous Update of same record" Watch "Simultaneous Update of same record" New topic
Author

Simultaneous Update of same record

Kavita Bopardikar
Greenhorn

Joined: Oct 16, 2000
Posts: 13
How can i ensure that one record is not updated by more than one user at the same time.
e.g. suppose i have a product table with fields product ID, product name, rate.
One user has taken up record having product id = 5 for updating the rate.
Another user has taken up the same record with product id = 5 for updating the product name.
How can I ensure that the commit for change of product rate is not overwritten by the commit for change of product name ?


Kavita
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
What about synchronizing the update method or block of code. I believe using the synchronize keyword on the method/code in question will achieve what you want.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Depends on how you are doing the update. Synchronization may not help. User 1 is browsing the row and making changes to one field. User 2 is making changes to another field. When you save the row, you save the wohole row not just the changed fields, Last in wins. What you can do is save the last update date/time as part of the record that you are showing to the user. If the last update date/time has changed since you showed the row to he user, then you know that someone else has changed the row. So then you can abort the update and show the user the changed row.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Kavita Bopardikar
Greenhorn

Joined: Oct 16, 2000
Posts: 13
Thanks for your replies folks !
I have found a solution to the problem.
The one mentioned by Thomas Paul will work. But, there's a slight catch in this one. There's always a chance that one user may save without making changes. In that case, the update date/time will be changed & another user who has actually changed the values will get a message that the record is changed & will have to requery the record only to find that there are no changes to it & his work of having made changes is futile.
The solution i have in mind is - before commiting the changes to the table, compare the current contents of that record with the ones sent by user. If there is even one change, then flash a message that the record has been changed & requery to see latest one. The advantage of this that the earlier problem of incorrect message of the record being changed is not displayed & genuine changes are committed & secondly, if at all user insists on getting a list of fields changed, well, it can be generated.
So folks, thanks once again for your answers. I hope my reply has also given you some more ideas about this thing. In case you have any more ideas about this one, please post your replies. I am looking for an efficient method. If one exists, it can be implemented.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
There's always a chance that one user may save without making changes.
The answer, of course, is to not update the row if there are no changes.
[This message has been edited by Thomas Paul (edited December 08, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simultaneous Update of same record