I am using Hibernate version 2.1.7c.
I am using the session-per-transaction pattern with a CMT bean.
I am load testing the application and have the following code executing in a non-transactional bean.
Code:
while (!done && tries<MAX_TRIES) {
try {
transactionalBean.persist( container.getCurrentState() );
done = true;
} catch (RuntimeException e1) {
if (isDeadlockException(e1)) {
LogFactory.getLog(this.getClass()).error("Retrying after Deadlock("+tries+")", e1);
tries++;
} else {
throw e1;
}
}
}
I get the following error intermittently:
Caused by: net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at net.sf.hibernate.collection.PersistentCollection.setCurrentSession(PersistentCollection.java:257)
at net.sf.hibernate.impl.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:38)
at net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
at net.sf.hibernate.impl.AbstractVisitor.processValues(AbstractVisitor.java:36)
at net.sf.hibernate.impl.AbstractVisitor.process(AbstractVisitor.java:91)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1465)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1479)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1402)
at gigo.database.hibernate.HibernateDatabase.saveOrUpdate(HibernateDatabase.java:91)
I have similar code executing from the web container and never see this exception.
It seems that when the CMT transaction is rolled back, the session is not finalized before the exception reaches the client and the second session is created.
Is there a way for me to ensure that the first session is destroyed before I raise the exception from the transactional method?