File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Connection Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connection Pool" Watch "Connection Pool" New topic

Connection Pool

J Craig

Joined: May 04, 2004
Posts: 15
Repost wrong forum sorry
I have a question about when to initialize the connection pool currently I am using a class for connection pooling and it is not performing correctly because multiple instances are being created. I am using a mvc approach and only have one servlet into the webApp. Can anyone tell me where I should initialize the connection pool object In the case of JNDI and a datasources I pose the same question How do I make sure there is only one reference so that pooling works correctly
My design is (jsp/html)TO controllerServlet(decide where to go instantiate helper class using command pattern) TO helperObject(performs specific task inserts updates deletes,etc as well as passing back to the controllerServlet the next jsp/html page to be forwarded to).

Jeff Walker
Ranch Hand

Joined: Apr 25, 2004
Posts: 116
A previous poster recommended the Singleton pattern; I agree, that will ensure only one instance is created.
As for the 'right place', if by connection pool, you refer to a global pool that any servlet (or JSP page or resource) in your app can request a connection from, then a class that implements the ServletContextListener interface should be created, and when the ServletContext is initialized by the servlet container, the connection pool can be created in your implementation of
public void contextInitialized(ServletContextEvent sce);
Don't forget to destroy (or is that release?) the connection pool in public void contextDestroyed() of the same class, to clean up your resources after use.
If your app has only one servlet, and that's the only place a connection will ever be called from, (you'd better be sure), then you could create the pool in the servlet's init() method, and set an attribute in the ServletContext object that references the pool. I actually don't recommend it, but it would work and allow you to debug it easily before you created a listeenr class for the ServletContextListener interface.
-jeff walker
J Craig

Joined: May 04, 2004
Posts: 15
Thanks Jeff I do have a singleton class in which all database access is done
will using the connection pool as a member of this class suffice.
I agree. Here's the link:
subject: Connection Pool
It's not a secret anymore!