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
Lew - 18 Oct 2007 05:39 GMT
> 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,
It's not the best way, and is the reason you're having so much trouble.
> 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.
finalize() is anathema. Avoid it at all costs. It is certainly not the way
to release resources.
> I suppose I could just instantiate a separate SessionFactory in each
> servlet. Just seems like overkill.
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.

Signature
Lew
Owen Jacobson - 18 Oct 2007 06:32 GMT
> > I suppose I could just instantiate a separate SessionFactory in each
> > servlet. Just seems like overkill.
[quoted text clipped - 4 lines]
> 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.
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.
Jean-Baptiste Nizet - 18 Oct 2007 12:46 GMT
Scott Harper a écrit :
> Ok, thanks for all the feedback... I knew *not* calling
> SessionFactory.close() was probably not very clean. However, I'm not
[quoted text clipped - 9 lines]
> close the SessionFactory when any particular servlet terminates, as
> others may still be active.
Maybe you could use a ServletContextListener, which is called when the
webapp is initialized and when the webapp is destroyed.
JB.
Adam Maass - 21 Oct 2007 06:19 GMT
> Ok, thanks for all the feedback... I knew *not* calling
> SessionFactory.close() was probably not very clean. However, I'm not
[quoted text clipped - 5 lines]
> method, and closing it in destroy(). With multiple servlets, however,
> things change.
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