wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlets - Help required urgently ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets - Help required urgently !" Watch "Servlets - Help required urgently !" New topic
Author

Servlets - Help required urgently !

yoginee Pathak
Greenhorn

Joined: Apr 25, 2001
Posts: 3
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
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
you ever heard of a singleton?
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
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
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlets - Help required urgently !
 
Similar Threads
Preloaded servlet
Connection Pool
Can I Pool Connections to DB Across JSPs?
Maintaining scalability in servlets programming
Re: Closing database connection in destroy metod()?