Re: Cleaning up after static member variables?
On Oct 16, 7:46 pm, sharp...@austin.dot.rr.dot.com (Scott Harper)
wrote:
I have a static member variable, in this particular case a Hibernate
SessionFactory. It is initialized in a static block. The
SessionFactory class has a close() method, which can free up resources
related to the factory...
How/where is the best place to call SessionFactory.close() on this
static object?
Or should I just not worry about calling close()?
scott
Your problem is not "how do I clean up static crud when the app shuts
down". It's "how do I make the SessionFactory available to everyone
while still retaining control over it?". Making it static was, IMO,
the wrong decision, for reasons you just discovered.
Odds are good that only a handful of classes actually need the
SessionFactory, but they're annoyingly far from the ones main() calls
to do work. The main() method, either directly or indirectly, should
provide the SessionFactory directly to those classes (so you don't
have to pass it down through every class that uses the classes that
use the session factory to get it where it needs to go), and main()
should arrange for it to be closed at shutdown (either at the end of
main, for simple apps, or in the window event handlers leading to
shutdown, for GUI apps).
This is what dependency injection frameworks do for you; doing it
yourself is tedious but not hard.