hi i have a servlet that save records in 3 different tables in the database in post method. i want to commit only after records are saved in all the 3 tables successfully..so i have set autocommit to false and used commit only after records r saved in all the 3 tables. i want to know that in case of a multi-threaded environment, will this pose a problem...for ex. if commit is called for 1 user when the other user is in middle of saving the record.. Also how should i solve this problem? do i require to use synchronize()...and if so what block of code should i put in it? thanks.. Nidhi
No, both users will run different transactions, means both users will have different connection objects. One connection object must be used by One thread at a time.
So, commit or rollback belongs to the session or connection. if you call commit on one connection then the other connections wouldn't do commit to their transactions.
But yes you have to synchronize this all, by putting the code inside synchronize block. Because suppose if some of the column has unique constraint or primary key. Then if a particular record gets inserted in first table and some other user inserted some other record in the second table with the same unique column value. Then the record which is inserted previously in the first table can't get inserted in second one. Hope you will get my point.
Although, i dont know the relationship between your three tables. And i dont know whether a user can access those other two tables directly or not.
if i have some class member variables (not a part of get or post methods), then can that pose to be a problem in case of multiple users? i have 2 such class varibles which i have initialized to null initially. i am assigning some value to them in get method and then accessing this value in the post method. Would declaring them as private can be a solution?
Joined: Aug 15, 2004
I think the class you are talking about is your servlet. and there are 2 class or instance variables.
Note: only local variables are guaranteed to be thread-safe. And about instance or class variables, you need to make them thread-safe by yourself.