A singleton is only a singleton to the classloader it lives in. A webapp is loaded in its own classloader, so when it is start/stopped or reloaded - a new classloader is made. And so is a brand new set of singletons.

The easy workaround is to create a ServletContextListener which listens for the webapp to be stopped. At that time - your can free the pool.

But I don't recall if ServletContextListener is available in the 2.2 servletapi.

-Tim

Matt E wrote:
Hello All!

Our team here has written a simple Connection Pool
class that is a singleton.

I've noticed that whenever Tomcat reloads my webapps
context (when I add a new class or something like
that), that it the next call to the Connection Pool
get instance method doesn't see the previous
singleton, and hence, it allocates a bunch of new
connections.

This would be fine, except that the old connections
are never closed (it seems that the Garbage Collector
doesn't eat them up) so I think that the old instance
of the singleton is still floating around in the JVM
somewhere.

This causes all the free sessions on the Database to
be eaten up, forcing a restart of tomcat.  Once tomcat
is restarted, all the connections are closed out
(since the JVM terminated, I guess) and things are
fine again.

What do I need to do so that the old connections are
removed?  This is on Tomcat 3.3.1a.  Here is the
relevent code of the Singleton getInstance method:

    public static synchronized ConnectionPool
getInstance(ConnectionPoolConstants c) throws
SQLException {

constants = c;

        if (instance == null) {
            // Determine which database url to connect
            db_url = constants.getTestDBURL();
            String line = "";
            try {
                Vector return_vec =
DelphiRuntime.execCommandWithOut("hostname");
                Enumeration return_enum =
return_vec.elements();
                while (return_enum.hasMoreElements())
{
                    line = (String)
return_enum.nextElement();
                }
            } catch (InterruptedException ire) {

} catch (IOException ioe) {

}

            if
(line.equals(constants.getProductionServerHostname()))
{
                db_url =
constants.getProductionDBURL();
            }

            instance = new ConnectionPool();
        }
        return instance;
    }

Thanks for any insights!

Cheers!


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to