Cleaning up after static member variables - revisited

S

Scott Harper

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
 
L

Lew

Scott said:
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.
 
O

Owen Jacobson

Scott Harper wrote:

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.

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.
 
J

Jean-Baptiste Nizet

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
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.

Maybe you could use a ServletContextListener, which is called when the
webapp is initialized and when the webapp is destroyed.

JB.
 
A

Adam Maass

Scott Harper said:
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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top