Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Conceptual doubt about concurrent access ....

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way is to use EJB:s for you DB connection, that gives you both transactions and thread safety.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic