Cleaning up after static member variables - revisited

Discussion in 'Java' started by Scott Harper, Oct 18, 2007.

  1. Scott Harper

    Scott Harper Guest

    Ok, thanks for all the feedback... I knew *not* calling
    SessionFactory.close() was probably not very clean. However, I'm not
    sure all the suggestions really apply to this particular situation --
    let me explain.

    This is a web app that has a collection of servlets. Before I had
    multiple servlets, I was building the SessionFactory in the init()
    method, and closing it in destroy(). With multiple servlets, however,
    things change.

    There is no main() function (that I have control over)... and I can't
    close the SessionFactory when any particular servlet terminates, as
    others may still be active.

    I don't like the complicated static initialization either. In this case
    though, the exceptions are caught and manifest themselves as
    ExceptionInInitErrors (or whatever that one is called), so it is pretty
    obvious when the servlets don't start. Most (all?) of the exceptions
    I'm catching are runtime exceptions anyway. I think the only one I can
    really get is the HibernateException.

    I originally went with the singleton approach. That might still be the
    best way... but even then, how do I know when the object is going away,
    so I can close the SessionFactory? I read a little about the finalize()
    method, but it doesn't seem that it is really applicable to that type of
    cleanup.

    I suppose I could just instantiate a separate SessionFactory in each
    servlet. Just seems like overkill.

    Any other ideas?

    thanks
    scott
     
    Scott Harper, Oct 18, 2007
    #1
    1. Advertisements

  2. Scott Harper

    Lew Guest

    It's not the best way, and is the reason you're having so much trouble.
    finalize() is anathema. Avoid it at all costs. It is certainly not the way
    to release resources.
    What is being overkilled? Creating factory objects is very low in overhead.

    What you're going through in order to preserve a magic "singleton" pattern is
    the overkill. Let each servlet have its own factory, for Pete's sake. I'll
    bet you'll find quality goes sharply up and nothing else is sacrificed.
     
    Lew, Oct 18, 2007
    #2
    1. Advertisements

  3. Alternately, if his container supports it, he could have the container
    manage the SessionFactory and place it in JNDI for his servlets to
    access. This is pretty similar to one of the standard ways of using
    JPA in a managed environment, where the EntityManagerFactory (rather
    than an EntityManager) is in JNDI.
     
    Owen Jacobson, Oct 18, 2007
    #3
  4. Scott Harper a écrit :
    Maybe you could use a ServletContextListener, which is called when the
    webapp is initialized and when the webapp is destroyed.

    JB.
     
    Jean-Baptiste Nizet, Oct 18, 2007
    #4
  5. Scott Harper

    Adam Maass Guest

    If all of the servlets reside in the same webapp, then the proper hook is a
    ServletContextListener. (These are configured in the <listener> stanza of
    teh web.xml.) A ServletContextListener is instantiated and its
    contextInitialized method is guaranteed to be called before any request goes
    to any servlet. Likewise, the ServletContextListener's contextDestroyed
    method is guaranteed to be called (at least when there's a clean shutdown)
    after all servlets stop accepting requests but before the webapp is
    unloaded.

    -- Adam Maass
     
    Adam Maass, Oct 21, 2007
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.