Results 1 to 5 of 5
- 11-26-2010, 09:35 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
do we need multiple connection objects
Heys all, i have a class with a static reference to a java.sql.Connection object. i mainly call the object's prepareStatement().
Every other servlet that needs the DB will basically contact this class and they will all share the same connection object. I'm wondering if i'm going to bump into some real concurrency issues or is it perfectly alright to do so?
- 11-27-2010, 01:18 AM #2
I'm pretty sure that this is a bad idea to do. because it's possible to get into a state where the connection is in use by one thread (request from a given user), because each servlet request is invoked in its own thread right. Any failure or jammed state from one request would also effect every user as well.
The usual way is to have a connection per request (either fetch it inside the servlet as needed, or create a threadLocal object). where the connection is fetched from a DataSource, and when we are finished with the connection object in our servlet, we invoke the connection.close() method, which doesn't actually close the connection, but usually releases it back to the JDBC connection pool, such as the commons-pool project that is bundled with tomcat.
Additionally, we usually configure the credentials for the data source in the context.xml or the servlet container (e.g. server.xml) as a JNDI data source name, where in our application we declare a reference to this JNDI named object to retreive the DataSource instance, and from there in each servlet where needed invoke DataSource.getConnection().
- 11-27-2010, 04:17 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
heys, btw just to check, is it true that if we do not explicitly connection.close then the connection will be closed automatically after x seconds?
- 11-27-2010, 03:00 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Entirely depends on the database settings.
If you don't explicitly close the connections on a webapp you will almost certainly hit the connection wall and be unable to connect...unless the webapp does very little.
You should always explicitly close resources you open.
As already said for your original question, do not share a connection within a multi-threaded environment. They are not thread safe.
- 11-27-2010, 06:16 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Instantiating Multiple Graphics objects
By theCardboardBox in forum New To JavaReplies: 2Last Post: 11-25-2010, 01:26 PM -
Multiple Graphics Objects?
By MrFish in forum Java 2DReplies: 7Last Post: 10-29-2010, 07:37 PM -
CMT and Connection Objects
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 10-03-2009, 06:08 PM -
how to deserialize multiple objects in a file
By xcallmejudasx in forum Advanced JavaReplies: 11Last Post: 12-16-2008, 05:29 PM -
Can I store multiple objects in an array
By lareauk in forum New To JavaReplies: 9Last Post: 05-29-2008, 03:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks