aspose file tools*
The moose likes Servlets and the fly likes Should we always synchronize db operations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Should we always synchronize db operations?" Watch "Should we always synchronize db operations?" New topic
Author

Should we always synchronize db operations?

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
In my web application, I have an 'Employee' table in the Database which is common for some users of the appln. This employees info can be modified by some users. (All add/delete/modify/list operations). I would like to know which portions of the coding should be synchronized like that.
Here is a use case:
1. A user views the list of Employees
2. He chooses to modify one field of the Employee
3. He enterd the new data in the HTML form and submits
4. The servlets does all the needed work for updating the info in Employees Table by means of EmployeesBank.updEmployee(Employee emp) method.
5. If the updation is success the servlet returns a page with the 'current' data of the Employee in the Employees table.
What I want to know is, the same 5 steps may be done for another user's request which might be 'MODIFY' operation for the SAME Employee info Isn't? So what happens if the JVM completes the first 4 steps of the 1st user and then does ALL 5 operations of the 2nd user's request and finally completes the 5th step of first user? Now the servlet will show a 'The following info is updated' page BUT with DIFFERENT details (which are of the 2nd user).
So I think we need to synchronize 'the set of operations'. Otherwise, there may be failure of data integrity.
Can you give some more advice on the synchronization part. Which portions of a servlet should be synchronized? Thank you.
[ I use ConnectionPool, no instance/static data which might be shared by all users. The connection pool is private instance member which has a pool of connections and is used to get/use/free connections ]
regds
maha anna
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This really should be handled by the database itself. Remember that synchronization only affects threads run from the same JVM, and says nothing about threads in other JVMs or other non-java applications. Even if you have no other business applications which use the database, there are almost always admin tools which can perform changes while other applications are running, so synchronization is a red herring in this case.
Most good databases (and most good JDBC drivers) support the concept of two-phase commit, where an operation is first performed "virtualy", and then committed (or "rolled back") as a single atomic operation.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
 
subject: Should we always synchronize db operations?