Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conceptual doubt about concurrent access ....

 
Celina Joseph
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic