Hi all, I've a class written for connection pooling. If i instantiate this class in every servlet it creates those many no. of pools. What should i do so that there's only one pool and all the servlets get connection from this pool. I'm using apache Jserv Thankx in advance ! Yoginee
Write a servlet that instantiates your connection pool and places the pool into the ServletContext. Now all servlets that share that ServletContext have access to one instance of your connection pool class. I think some servers can specify servlets to run on startup, and this is what you would want to do with the servlet that instantiates your connection pool. Jason
What would happen if you reload the servlet? I mean how would it know to close all of the connections. I think a singleton is a better idea. ex.. public class ConnectionManager { private static ConnectionManager cm; private ConnectionManager() { \\initialize the object } public static ConnectionManager getConnectionManager() { if(cm == null) { synchronized(ConnectionManager.class) { cm = new ConnectionManager(); } } return cm; } }
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
posted
0
I should have been more specific. Initialize the connection pool in the init() method of the servlet. This will ensure that it is only instantiated once. You can make it a singleton if you want to, but it really shouldn't matter in this particular case. An alternative method is to make it a singleton class, but then have every class which tries to use the pool first check and see if it is in the ServletContext, and if not place it in the ServletContext. I prefer having one servlet do the work because it puts less overhead on the developers. Keep in mind that for multiple servlets to share the same connection pool it must be stored in the ServletContext (application scope in JSPs). Just initializing a singleton class in a servlet will not make it available to other servlets if it is not placed in the ServletContext. This of course assumes that you are using only a web server and do not have access to an application server, in which case EJB might be the way to go. Jason [This message has been edited by Jason Menard (edited June 27, 2001).]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.