Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

A singleton lifespan in Tomcat

Thread view: 
Alexandre Brizard - 22 Dec 2005 14:44 GMT
Hi!

I have a singleton class that operates on a Tomcat server.  When I
start Tomcat, everything is fine, I have only one instance of the
class.  But, if I modify the web.xml file, per example, and the context
is stopped then restarted, the instance I had in the first place isn't
destroyed and continues to run, and another singleton is created when I
call the getInstance() method of the singleton.  How do I fix that?

Here are some excerpts of my class:

public class ConnectionPool {

private static ConnectionPool instance = null;
.
.
.
public synchronized static ConnectionPool getInstance()
                         throws SQLException, ClassNotFoundException {
         if (instance == null){
             instance = new
ConnectionPool(m_driverName,m_dbURL,m_user,m_password);
         }
         return instance;
}
.
.
.
};

Thanks!
Chris Smith - 22 Dec 2005 15:49 GMT
> I have a singleton class that operates on a Tomcat server.  When I
> start Tomcat, everything is fine, I have only one instance of the
> class.  But, if I modify the web.xml file, per example, and the context
> is stopped then restarted, the instance I had in the first place isn't
> destroyed and continues to run, and another singleton is created when I
> call the getInstance() method of the singleton.  How do I fix that?

Both of the basic singleton implementations in Java rely on the
uniqueness of static variables.  When you load a new web application in
Tomcat (including if you modify a file that convinces Tomcat to reload
your web app for you), Tomcat will create a new servlet context class
loader, and the new class loader will load new classes that have their
own static variables.

Since the new web application will not have access to the old instance,
you need to swap over to the new instance.  That probably means properly
closing your old instance when you're finished with it.  Try adding a
listener to the servlet context to do that.

Google turns this up:

> http://www.stardeveloper.com/articles/display.html?article=2001111901&page=1

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.