Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Singleton in Tomcat web app

843836Feb 15 2005 — edited Feb 16 2005
Hi folks,
This is a very obscure problem, and one that may not be suitable for this forum. If so, I apologise and pointers to a more appropriate one would be welcome.

I am writing a new webapp using Tomcat 3.3 as my servlet/JSP container. At the outset, I wanted to have a globally available logging facility, available from throughout the system, not just servlets/JSPs, so I decided to use a singleton class, that would be loaded when Tomcat starts using the <load-on-startup> tag in the web.xml file.

I have a servlet that extends HttpServlet that just has a single init() method which loads the singleton with the path to the logger directory and the singleton itself is something like this:
public class IliadLogger
{
    private static IliadLogger me;
    
    private Logger mLogger;
    private FileHandler mFileHandler;
    
    /**
     * Instantiates the logger for the Iliad application
     */
    private IliadLogger(String initPath)
    {
        // code to initialise logger here
    } // end constructor

    /**
     * Returns a logger for the Iliad application.  This is a singleton object.  This factory method
     * will also be able to instantiate the logger properly as well.
     * 
     * @param initPath the (absolute) path to the webapp Web-inf/ directory
     * @return a logger for the Iliad application
     */
    public static IliadLogger getInstance(String initPath)
    {
        if (me == null)
            me= new IliadLogger(initPath);

        return me;
    } // end method getInstance();
} // end class IliadLogger
And in my initialiser, I have a code snippet like this:
IliadLogger.getInstance(getServletContext().getRealPath("/Web-inf/"));
(after this, I can just use an IliadLogger.getInstance() method with no argument to return the logging object).

My problem (finally!) is that whenever the webapp is reloaded (if a class changes etc), the logger seems to be destroyed and when my init code is executed, it runs the constructor code again, when (as I understand it), it shouldn't, since the VM hasn't been shut down, so the object should still be in memory.

I'm using J2SDK 1.4.2_05

Any ideas?
Raj.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 16 2005
Added on Feb 15 2005
4 comments
438 views