tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcel Stor <mar...@frightanic.com>
Subject RE: Tomcat reload / classloader / connection pool
Date Wed, 03 Dec 2003 00:21:09 GMT
Christopher Schultz wrote:
> Marcel,
> 
> > The problems with Tomcat occur upon reload of the web app. The
> > classloader reloads the whole app and all my DB connections get
> > opened again. This wouldn't cause much pain if the existing
> > connections were garbage collected i.e. finalized. This doesn't
> > happen and after a couple of reloads the DB's max connections limit
> > is reached... 
> 
> Where do the class files for your DB connection pool live?
> 
> Is there a reason to use your own home-brewed CP other than bragging
> rights? :) 

I was afraid this question would be raised ;-)
To be honest, I'm short on reasons. I might have to abandon my own dear
CP in the future.

> > First of all, why is this so?
> 
> This is likely because of the way you use the singleton. When you
> have a "singleton" (I use quotes because it's probably not a real
> singleton.... otherwise we would not be having this discussion) the
> static data is associated with the java.lang.Class object that gets
> created when your class is loaded. When the re-load occurs, that
> static data sticks around. The new classloader used for the new
> context loads a new copy of the "singleton" and they pile up over
> time. You need to shutdown the connection pool before the context
> dies.

I understand it sticks around because the data is independent of the any
object. But eventually even this static data should get garbage
collected.

Well, if this is not a GoF-Singleton what is it then...?

public class ConnectionFactory {
    private static Logger logger =
Logger.getLogger("ConnectionFactory");
    private static ConnectionFactory instance;
    //stores references to all connection managers (connection pools)
    private final Hashtable managers = new Hashtable();

    private ConnectionFactory() {
    	//foo
    }
    public static ConnectionFactory getInstance() {
        if (null == instance) {
            instance = new ConnectionFactory();
        }
        return instance;
    }
}

> > Second of all, how can I prevent this? Somehow listen for reloads
> > and react appropriately?
> 
> Yes. Consider writing a ServletContextListener and "closing" the pool
> before the context goes down. It will be run when the new context
> comes up, too.
> 
> Check the documentation for javax.servlet.ServletContextListener

I've just gone through its description. Looks my friend for this
problem.

> > Instead of deploying the framework JAR in the web app's WEB-INF/lib
> > directory I could place somewhere in the regular CLASSPATH, but
> > then all web apps would have access to the same ConnectionFactory
> > :-(( 
> 
> Yes, and the problem would get worse.

Exactly.

Regards,
Marcel



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message