You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Matt E <mg...@yahoo.com> on 2003/08/04 20:07:41 UTC

Singleton and Reloadable Contexts.

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!

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Re: Singleton and Reloadable Contexts.

Posted by Tim Funk <fu...@joedog.org>.
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!
>  


Re: Singleton and Reloadable Contexts.

Posted by Tim Funk <fu...@joedog.org>.
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Faking ServletContextListener with Tomcat 3.3.1a? [Was: Singleton and Reloadable Contexts.]

Posted by Matt E <mg...@yahoo.com>.
After looking through the archives more (why is it
that you always find something after you've asked.)  I
found this message:
http://mikal.org/interests/java/tomcat/archive/view?mesg=51529

That's exactly what I want to do.  However the server
is running Tomcat 3.3.1a.  Short of updating the
server, is there anyway to fake the
contextInitialized() and contextDestoryed() methods in
the Older Tomcat?

Cheers,
Matt G. Ellis
--- Matt E <mg...@yahoo.com> 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!
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site
> design software
> http://sitebuilder.yahoo.com
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tomcat-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Faking ServletContextListener with Tomcat 3.3.1a? [Was: Singleton and Reloadable Contexts.]

Posted by Matt E <mg...@yahoo.com>.
After looking through the archives more (why is it
that you always find something after you've asked.)  I
found this message:
http://mikal.org/interests/java/tomcat/archive/view?mesg=51529

That's exactly what I want to do.  However the server
is running Tomcat 3.3.1a.  Short of updating the
server, is there anyway to fake the
contextInitialized() and contextDestoryed() methods in
the Older Tomcat?

Cheers,
Matt G. Ellis
--- Matt E <mg...@yahoo.com> 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!
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site
> design software
> http://sitebuilder.yahoo.com
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tomcat-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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