are you saving at application level session and/or sessionfactory? if not at what level are you saving them? Are you using 2nd level caching?
package gov.fws.rmad.util;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.commons.logging.*;
import javax.naming.*;
/**
* Basic Hibernate helper class for Hibernate configuration and startup.
* <p>
* Uses a static initializer to read startup options and initialize
* <tt>Configuration</tt> and <tt>SessionFactory</tt>.
* <p>
* This class also tries to figure out if JNDI binding of the <tt>SessionFactory</tt>
* is used, otherwise it falls back to a global static variable (Singleton). If
* you use this helper class to obtain a <tt>SessionFactory</tt> in your code,
* you are shielded from these deployment differences.
* <p>
* Another advantage of this class is access to the <tt>Configuration</tt> object
* that was used to build the current <tt>SessionFactory</tt>. You can access
* mapping metadata programmatically with this API, and even change it and rebuild
* the <tt>SessionFactory</tt>.
* <p>
* If you want to assign a global interceptor, set its fully qualified
* class name with the system (or hibernate.properties/hibernate.cfg.xml) property
* <tt>hibernate.util.interceptor_class</tt>. It will be loaded and instantiated
* on static initialization of HibernateUtil; it has to have a
* no-argument constructor. You can call <tt>HibernateUtil.getInterceptor()</tt> if
* you need to provide settings before using the interceptor.
* <p>
* Note: This class supports annotations by default, hence needs JDK 5.0
* and the Hibernate Annotations library on the classpath. Change the single
* commented line in the source to make it compile and run on older JDKs with
* XML mapping files only.
* <p>
* Note: This class supports only one data store. Support for several
* <tt>SessionFactory</tt> instances can be easily added (through a static <tt>Map</tt>,
* for example). You could then lookup a <tt>SessionFactory</tt> by its name.
*
* @author
[email protected] */
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final String INTERCEPTOR_CLASS = "hibernate.util.interceptor_class";
private static Configuration configuration;
private static SessionFactory sessionFactory;
static {
// Create the initial SessionFactory from the default configuration files
try {
// Replace with Configuration() if you don't use annotations or JDK 5.0
configuration = new Configuration();
// This custom entity resolver supports entity placeholders in XML mapping files
// and tries to resolve them on the classpath as a resource
configuration.setEntityResolver(new ImportFromClasspathEntityResolver());
// Read not only hibernate.properties, but also hibernate.cfg.xml
configuration.configure("/WEB-INF/xml/rmad_hibernate.cfg.xml");
configuration.addClass(gov.fws.rmad.domain.OperatorDO.class);
configuration.addClass(gov.fws.rmad.domain.ManagementUnitDO.class);
configuration.addClass(gov.fws.rmad.domain.ManagementActionDO.class);
// Set global interceptor from configuration
setInterceptor(configuration, null);
if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
// Let Hibernate bind the factory to JNDI
configuration.buildSessionFactory();
} else {
// or use static variable handling
sessionFactory = configuration.buildSessionFactory();
}
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the original Hibernate configuration.
*
* @return Configuration
*/
public static Configuration getConfiguration() {
return configuration;
}
/**
* Returns the global SessionFactory.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
SessionFactory sf = null;
String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
if ( sfName != null) {
log.debug("Looking up SessionFactory in JNDI.");
try {
sf = (SessionFactory) new InitialContext().lookup(sfName);
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
} else {
sf = sessionFactory;
}
if (sf == null)
throw new IllegalStateException("SessionFactory not available.");
return sf;
}
/**
* Closes the current SessionFactory and releases all resources.
* <p>
* The only other method that can be called on HibernateUtil
* after this one is rebuildSessionFactory(Configuration).
*/
public static void shutdown() {
log.debug("Shutting down Hibernate.");
// Close caches and connection pools
getSessionFactory().close();
// Clear static variables
configuration = null;
sessionFactory = null;
}
/**
* Rebuild the SessionFactory with the static Configuration.
* <p>
* This method also closes the old SessionFactory before, if still open.
* Note that this method should only be used with static SessionFactory
* management, not with JNDI or any other external registry.
*/
public static void rebuildSessionFactory() {
log.debug("Using current Configuration for rebuild.");
rebuildSessionFactory(configuration);
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
* <p>
* HibernateUtil does not configure() the given Configuration object,
* it directly calls buildSessionFactory(). This method also closes
* the old SessionFactory before, if still open.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg) {
log.debug("Rebuilding the SessionFactory from given Configuration.");
synchronized(sessionFactory) {
if (sessionFactory != null && !sessionFactory.isClosed())
sessionFactory.close();
if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null)
cfg.buildSessionFactory();
else
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
}
}
/**
* Register a Hibernate interceptor with the current SessionFactory.
* <p>
* Every Session opened is opened with this interceptor after
* registration. Has no effect if the current Session of the
* thread is already open, effective on next close()/getCurrentSession().
* <p>
* Attention: This method effectively restarts Hibernate. If you
* need an interceptor active on static startup of HibernateUtil, set
* the <tt>hibernateutil.interceptor</tt> system property to its
* fully qualified class name.
*/
public static SessionFactory registerInterceptorAndRebuild(Interceptor interceptor) {
log.debug("Setting new global Hibernate interceptor and restarting.");
setInterceptor(configuration, interceptor);
rebuildSessionFactory();
return getSessionFactory();
}
public static Interceptor getInterceptor() {
return configuration.getInterceptor();
}
/**
* Resets global interceptor to default state.
*/
public static void resetInterceptor() {
log.debug("Resetting global interceptor to configuration setting");
setInterceptor(configuration, null);
}
/**
* Either sets the given interceptor on the configuration or looks
* it up from configuration if null.
*/
private static void setInterceptor(Configuration configuration, Interceptor interceptor) {
String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
if (interceptor == null && interceptorName != null) {
try {
Class interceptorClass =
HibernateUtil.class.getClassLoader().loadClass(interceptorName);
interceptor = (Interceptor)interceptorClass.newInstance();
} catch (Exception ex) {
throw new RuntimeException("Could not configure interceptor: " + interceptorName, ex);
}
}
if (interceptor != null) {
configuration.setInterceptor(interceptor);
} else {
configuration.setInterceptor(EmptyInterceptor.INSTANCE);
}
}
}
And here is the HibernateThreadFilter class that is a servlet filter in the web.xml
package gov.fws.rmad.app.servlet;
import org.apache.commons.logging.*;
import org.hibernate.*;
import gov.fws.rmad.util.HibernateUtil;
import javax.servlet.*;
import javax.servlet.Filter;
import java.io.IOException;
/**
* A servlet filter that provides a thread-bound session-per-request.
* <p>
* This filter should be used if your <tt>hibernate.current_session_context_class</tt>
* configuration is set to <tt>thread</tt> and you are not using JTA/CMT. You can use
* this filter for a thread-bound <tt>Session</tt>, either with resource-local transactions
* (direct JDBC) or user-managed JTA transactions. Set your
* <tt>hibernate.transaction.factory_class</tt> accordingly.
* <p>
* An alternative, more flexible solution is <tt>TransactionInterceptor</tt>
* that can be applied to any pointcut with JBoss AOP.
* <p>
* Note that you should not use this interceptor out-of-the-box with enabled optimistic
* concurrency control. Apply your own compensation logic for failed conversations, this
* is totally dependent on your applications design.
*
* @see org.hibernate.ce.auction.persistence.TransactionInterceptor
*
* @author
[email protected] */
public class HibernateThreadFilter implements Filter {
private static Log log = LogFactory.getLog(HibernateThreadFilter.class);
private SessionFactory sf;
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {
log.debug("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
log.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
} catch (StaleObjectStateException staleEx) {
log.error("This interceptor does not implement optimistic concurrency control!");
log.error("Your application will not work until you add compensation actions!");
// Rollback, close everything, possibly compensate for any permanent changes
// during the conversation, and finally restart business conversation. Maybe
// give the user of the application a chance to merge some of his work with
// fresh data... what you do here depends on your applications design.
throw staleEx;
} catch (Throwable ex) {
// Rollback only
ex.printStackTrace();
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!", rbEx);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
log.debug("Initializing filter, obtaining Hibernate SessionFactory from HibernateUtil");
sf = HibernateUtil.getSessionFactory();
}
public void destroy() {}
}