Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating connections and closing inside service methods - is it thread safe?

 
Mohamed Iqzas
Ranch Hand
Posts: 73
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside post method am creating new connection object(either by drivermanager or by datasource look-up) as a local variable inside the method.. and then I have a try block which executes statements and then finally block closes this connection object.

Now by the theory I have learnt, default method of servicing requests to a servlet is by creating new thread for each new request and these threads act on a single Servlet instance.

So if there are simultaneous users, their requests being served by a single servlet using different threads, all these threads act on same connection object, and if one thread closes the connection before the other thread uses it, it should throw an exception?

But while I test my application I didnt find any such issues. Is my assumption right? my assumption is - it will throw exception if simultaneous requests are being served by this post method... kindly suggest me what's the better way of closing my connection if this is wrong..

Thanks in advance!
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The connection pool must be reassigning a new connection to the requests if the previous connection is closed.

That's why you are not getting an error.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the reasons why you are recommended to use a Connection pool instead of brute-force DriverManager Connections.

Yes, each incoming request is attached to a discrete service thread. Yes, it is extremely unsafe to store data in static or instance variables in a Servlet class or in any other class invoked by that class. If you need per-instance (thread) storage, construct an object to hold it and put that object in J2EE's Request or Session scope. Request scope is completely thread-safe (not shared), Session scope is reasonably thread-safe, except in cases where a single client has dispatched multiple requests concurrently.

A side note, is that you should strive to minimize the amount of time you hold an open Connection and always explicitly close it, in order to avoid having a bunch of useless Connection objects floating around awaiting garbage collection. If they're open, even when orphaned, they're withholding critical database resources from the rest of the system.

The one thing you should avoid at all costs is anything that would serialize a Connection, such as placing it in Session scope for access by a later request. The Connection is an interface, not a class, and may not successfully survive serialization/deserialization.
 
Mohamed Iqzas
Ranch Hand
Posts: 73
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the vital info.

DBConnect is a class i use to look up datasource.

inside getConnection() method of DBConnect I have:




Inside post method of servlet I have:


still the Connection pool look-up done inside DBConnect. getConnection() method will assign a separate connection object for distinct requests?

Sorry if i am asking the same thing again. just to be sure of the concept please confirm this.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The connection pool itself is sharable.

What I typically do is look it up in a servlet init() method and store its object reference as a static property so that I don't have to to the JNDI stuff each time. Then you can code a static method to do the getConnection/setAutoCommit using that static pool reference to return a Connection. The static get Connection method can be invoked by thread-specific code as long as all it does is get a connection from the pool and not attempt to modify static objects.
 
Raj roy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

is it good practice create connection objects in GET/POST methods or we should create the connection in the init methods itself?

Please provide a sample example if possible.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't create Connections in either place. You should request Connections from the Connection Pool, which will return an existing Connection for your use or create one if all existing Pool-controlled Connections are in use (unless the pool limit has been exceeded).

You should never obtain a Pool Connection in an init() method with the idea of holding it. Always obtain the Connection right before you do a database operation, do the database operations, then close the Connection (which will return it to the Pool for the next requester). You should never hold onto a Connection any longer than you absolutely must, and especially not between HTTP requests.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic