Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should we always 'synchronize' the db operations?

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? Since I came up with the database operation I posted in JDBC forum. 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
 
Monty Ireland
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would synchronize the following methods:
start(), paint(), & methods that call repaint().
Please review pg 99 thru 104 of the JDBC API Tutorial for more info.
Code examples of OutputApplet.java & OutputApplet.html are contained in a zip file which can be found at http://www.java.sun.com/products/jdbc/book.html.
Hope this helps...
Let me know if you need more information.

------------------
We learn more from our mistake's than from our success's.
a.k.a. monty6
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure I answered this already, but it looks like it never made it here. Oh Well...
Anyway, this is really a responsibility of the database, and synchronizing a few methods is not going to solve the problem. Synchronizing only affects threads in the same VM, but the database can probably be concurrently accessed by other Java VMs, other non-Java applications and its own administration utilities, all of which are able to make sweeping changes which synchronization will not protect you from.
The solution is to use database transactions. You probably have "autocommit" set to true for your database accesses. If you are worried about these issues you should use manual commits and rollbacks to make sure that each of your changes is an atomic unit. Have another look at the JDBC spec and the documentation for your driver.
 
Monty Ireland
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree....
Prior post pretained to JVM threads only.
SQL DML should be commited as a unit of work and/or database
transaction.
If your transaction volumn is going to be high. Stay away
from JAVA BEANS. The overhead will kill your application. That is what has happened to two of my prior clients.
Work with your DBA to determine whak kind of isolation level you should be using. Be careful with this one.
FYI, use dirty read's whenever possible...

------------------
We learn more from our mistake's than from our success's.
a.k.a. monty6
[This message has been edited by Monty Ireland (edited November 09, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frank and Monty. Initially I posted here. SInce the response was little late , I posted again in Servlets forum and I got a response from you (Frank!) there. So I forgot to come here and check again. Once again thank you.
regds
maha anna
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooh, Maha. Multiple posting - you should know better!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic