The moose likes Servlets and the fly likes Conceptual doubt about concurrent access .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Conceptual doubt about concurrent access ...." Watch "Conceptual doubt about concurrent access ...." New topic
Author

Conceptual doubt about concurrent access ....

Celina Joseph
Greenhorn

Joined: Aug 23, 2001
Posts: 24
I create a Connection instance in my init() and destroy it in the destroy().
My servlet allows concurrent access.
My autoCommit() is false.And in my service(doPost), I start a transaction, perform multiple SQL, commit or rollback the transaction.
When another request comes at the time when one request is being executed, what happens ?
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73
I think, it would be a wrong of doing like this if u need transaction. If a new request arrives in the middle of a transaction of the previous request, then one thread(one request) may try to start transaction, while another thread(another request) may try to rollback the transaction. There r two ways to solve this. One is to synchronize the doPost method which is not good for a site with considerable hits. Another way is to open and close the connection in the doPost method. The ConnectionPooling mechanism must be used for obtaining quick connection to the database.
S.Mohamed Yousuff
Originally posted by Celina Joseph:
I create a Connection instance in my init() and destroy it in the destroy().
My servlet allows concurrent access.
My autoCommit() is false.And in my service(doPost), I start a transaction, perform multiple SQL, commit or rollback the transaction.
When another request comes at the time when one request is being executed, what happens ?
Peter Kristensson
Ranch Hand

Joined: Jul 02, 2001
Posts: 118
Another way is to use EJB:s for you DB connection, that gives you both transactions and thread safety.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Generally speaking you should NEVER use an instance variable in a servlet for data that changes with each user. Getting over this habit is one of the big mental hurdles on moving to servlet programming.
The second point is that practices you can get away with in a single user application that just runs once and stops are fatal in a servlet that is expected to run for days and handle hundreds of requests. Case in point, your creation of a Connection. If the database you connect to closes the connection your servlet has no way to recover. Thats why connection pool utility classes were invented.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conceptual doubt about concurrent access ....