Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets - Help required urgently !

 
yoginee Pathak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you ever heard of a singleton?
- satya
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic