Hibernate version: 3.0.5
I am accessing Hibernate through a HibernateUtil class patterned after the one on CaveatEmptor. The portions where I am having trouble are identical to what is on CaveatEmptor (see below)
Code:
package org.hibernate.auction.persistence;
// import ...
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
// ... session factory stuff omitted for brevity
public static Session getSession()
throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws InfrastructureException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread.");
s.close();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws InfrastructureException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
// ...
}
Basically there is a JUnit test that passes without problem when run standalone. When the test is placed inside a test suite, whichever test comes second fails when it tries to commit a transaction. The reason it fails is that the beginTransaction method finds an existing transaction that is tied to a different (closed) session which then fails with a "Session is closed" message.
Basically the jvm inside Eclipse which runs the tests is reusing the thread, so the threadLocals are stale. The HibernateUtil code (and mine, which is identical) treat the threadTransaction and threadSession variables as independent identities.
My question is this:
Is it reasonable to assume that the lifeline of a Transaction will be always entirely with the lifeline of a Session? If so, then it would be safe to rewrite the closeSession() method as follows:
Code:
public static void closeSession()
throws InfrastructureException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
// THIS IS MY SUGGESTED ADDITION
threadTransaction.set(null);
//END OF SUGGESTED ADDITION
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread.");
s.close();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
This would eliminate the possibility of a stale transaction being found. Would it have negative effects? Am I overlooking something?