How can my Seam app know when the Hibernate SessionFactory is closed in the org.hibernate.impl.SessionFactoryImpl class? I am using JPA 1.0 (EntityManager interface) with Hibernate as my persistence provider in a Seam 2.0.2 app. I want to perform an operation when the SessionFactory/EntityManagerFactory is closed. How can I implement this? In the Hibernate doc it references events and interceptors but not sure how to hook this up with Seam app and use @Observer method.
The motivation for this question is here:
https://www.hibernate.org/216.html. thx.
Code:
/**
* Closes the session factory, releasing all held resources.
*
* <ol>
* <li>cleans up used cache regions and "stops" the cache provider.
* <li>close the JDBC connection
* <li>remove the JNDI binding
* </ol>
*
* Note: Be aware that the sessionfactory instance still can
* be a "heavy" object memory wise after close() has been called. Thus
* it is important to not keep referencing the instance to let the garbage
* collector release the memory.
*/
public void close() throws HibernateException {
if ( isClosed ) {
log.trace( "already closed" );
return;
}
log.info("closing");
isClosed = true;
Iterator iter = entityPersisters.values().iterator();
while ( iter.hasNext() ) {
EntityPersister p = (EntityPersister) iter.next();
if ( p.hasCache() ) {
p.getCacheAccessStrategy().getRegion().destroy();
}
}
iter = collectionPersisters.values().iterator();
while ( iter.hasNext() ) {
CollectionPersister p = (CollectionPersister) iter.next();
if ( p.hasCache() ) {
p.getCacheAccessStrategy().getRegion().destroy();
}
}
if ( settings.isQueryCacheEnabled() ) {
queryCache.destroy();
iter = queryCaches.values().iterator();
while ( iter.hasNext() ) {
QueryCache cache = (QueryCache) iter.next();
cache.destroy();
}
updateTimestampsCache.destroy();
}
settings.getRegionFactory().stop();
if ( settings.isAutoDropSchema() ) {
schemaExport.drop( false, true );
}
try {
settings.getConnectionProvider().close();
}
finally {
SessionFactoryObjectFactory.removeInstance(uuid, name, properties);
}
observer.sessionFactoryClosed( this );
eventListeners.destroyListeners();
}