-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: SessionFactory.close() - where/how to call this from webapp?
PostPosted: Tue Apr 14, 2009 1:05 am 
Newbie

Joined: Mon Apr 13, 2009 7:45 am
Posts: 4
Hi

This might be a silly question, but where or how would one call the close() methods on the SessionFactory existing in a web application?

The SessionFactory gets created when the app starts up (not placing it in JNDI...), and the API docs state:

Quote:
Destroy this SessionFactory and release all resources (caches, connection pools, etc)


But from a webapp - how would you know when the application server is shutting down, or if you application is being stopped?

I've tried
Code:
Runtime.getRuntime().addShutdownHook(new Thread() { ... });
, but that didn't seem to work... (stopping Tomcat from inside Eclipse, stopping application from Tomcat manager (outside eclipse), or stopping Tomcat (outside eclipse)).

Placing that ShutdownHook in a standalone Java app does work...

There must be a common solution for this?

Any suggestions?

Thanks[/code]


Top
 Profile  
 
 Post subject: Solution!
PostPosted: Wed Apr 15, 2009 4:14 pm 
Newbie

Joined: Mon Apr 13, 2009 7:45 am
Posts: 4
Just thought I'd post the solution to this question... came upon the solution from a former colleague...

You need to create a class that implements the ServletContextListener interface. It will require you to implement 2 methods - one for when the context is created, and one when it is destroyed.

You then add this class in your web.xml as a listener...

From the contextDestroyed() method that you implement, you can call the close() method on your SessionFactory (to properly shut down the resources and cache).

This works when running Tomcat as a service or standalone, but didn't work while running Tomcat inside Eclipse... so - not too bad!

I found that after calling the close() method to properly shut-down the cache, my EHCache's disk persistence started working, and I noticed the cache-files growing upon shutdown. Previously - without calling the close() method on the sessionfactory, the cache-files remained empty.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.