wood burning stoves 2.0*
The moose likes Servlets and the fly likes A query...plz help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A query...plz help!!" Watch "A query...plz help!!" New topic
Author

A query...plz help!!

Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

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
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

thanks alot Adeel. your reply has been of great help.
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

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?

thanks
nidhi
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.

cheers.
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

thanks a lot for the reply. ya..i was talking about my servlet class.
i have declared some class variables in it (outside get or post method)
something like below..

public class cloc extends HttpServlet {
private String cn_id = null;
private String loc_no = null;

i have declared them as private. will this solve the problem in case of multiple users accessing the servlet?

thanks
Nidhi
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
Hi Adeel Ansari

"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".

How it affects same unique column value in two different tables ?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Nidhi Singhal:
i have declared them as private. will this solve the problem in case of multiple users accessing the servlet?


No, it doesn't solve your problem. they will remain unsafe. you have to make them thread-safe by synchronization.

All the requests are entertained by the same instance of a servlet.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by kri shan:
"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".

How it affects same unique column value in two different tables ?


Well, here is the scenerio.

first record, first table
____________
Adeel, 10, cheif system engineer
____________________

first records, second table by some other user
________________
Abdullah, 10, Director IT
______________________

Now if the number field here is unique for both the tables or for just the second table, then the first record can not get inserted in the second table.

hope you will get this.
thanks.
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

thanx alot adeeel..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A query...plz help!!