I've just converted my working Hibernate (2.1.3) application to a Hibernate - Spring application. Everything works fine until I try to update a record.
He throws the following
Exception
Code:
org.springframework.orm.hibernate.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
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:93)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1448)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1462)
at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1347)
at org.springframework.orm.hibernate.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:276)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:150)
at org.springframework.orm.hibernate.HibernateTemplate.update(HibernateTemplate.java:274)
at be.ngb.dao.HibernatePloegDAO.updatePloeg(HibernatePloegDAO.java:109)
at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:118)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:169)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
at $Proxy1.updatePloeg(Unknown Source)
at be.ngb.bo.PloegImpl.updatePloeg(PloegImpl.java:38)
at test.be.ngb.bo.TestPloeg.testUpdatePloeg(TestPloeg.java:127)
at com.intellij.rt.execution.junit2.JUnitStarter.main(Unknown Source)
at com.intellij.rt.execution.application.AppMain.main(Unknown Source)
Here is my
debug logCode:
[17:28:38,250] DEBUG net.sf.hibernate.transaction.JDBCTransaction - disabling autocommit
[17:28:38,265] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value [org.springframework.orm.hibernate.SessionHolder@5d4de6] for key [net.sf.hibernate.impl.SessionFactoryImpl@2f1bf0] to thread [main]
[17:28:38,281] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@27968] for key [org.apache.commons.dbcp.BasicDataSource@2cdd02] to thread [main]
[17:28:38,296] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Initializing transaction synchronization
[17:28:38,312] DEBUG be.ngb.dao.HibernatePloegDAO - Update ploeg : Team Rocket 2
[17:28:38,312] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value [org.springframework.orm.hibernate.SessionHolder@5d4de6] for key [net.sf.hibernate.impl.SessionFactoryImpl@2f1bf0] bound to thread [main]
[17:28:38,328] DEBUG net.sf.hibernate.impl.SessionImpl - updating [be.ngb.vo.PloegVO#e94ede61c3a2d49700bb869441efcc28]
[17:28:38,421] DEBUG org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on org.springframework.orm.hibernate.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
[17:28:38,453] DEBUG org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Winning rollback rule is: null
[17:28:38,453] DEBUG org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - No relevant rollback rule found: applying superclass default
[17:28:38,468] INFO org.springframework.transaction.interceptor.TransactionInterceptor - Invoking rollback for transaction on method 'updatePloeg' in class [be.ngb.dao.PloegDAO] due to throwable [org.springframework.orm.hibernate.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions]
[17:28:38,500] DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Triggering beforeCompletion synchronization
[17:28:38,500] INFO org.springframework.orm.hibernate.HibernateTransactionManager - Initiating transaction rollback
[17:28:38,515] DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Rolling back Hibernate transaction on session [net.sf.hibernate.impl.SessionImpl@347124]
[17:28:38,531] DEBUG net.sf.hibernate.transaction.JDBCTransaction - rollback
[17:28:38,531] DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
[17:28:38,531] DEBUG net.sf.hibernate.transaction.JDBCTransaction - re-enabling autocommit
[17:28:38,546] DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Triggering afterCompletion synchronization
[17:28:38,562] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Clearing transaction synchronization
[17:28:38,562] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@27968] for key [org.apache.commons.dbcp.BasicDataSource@2cdd02] from thread [main]
[17:28:38,578] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Removed value [org.springframework.orm.hibernate.SessionHolder@5d4de6] for key [net.sf.hibernate.impl.SessionFactoryImpl@2f1bf0] from thread [main]
[17:28:38,593] DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Closing Hibernate session [net.sf.hibernate.impl.SessionImpl@347124] after transaction
[17:28:38,656] DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Closing Hibernate session
[17:28:38,687] DEBUG net.sf.hibernate.impl.SessionImpl - closing session
[17:28:38,687] DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
[17:28:38,703] DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
the Java codeHibernatePloegDAO Code:
public void updatePloeg(PloegVO ploeg) throws DAOException, AlreadyExistsException {
Logger.debug(this, "Update ploeg : " + ploeg.getNaam());
getHibernateTemplate().update(ploeg);
}
PloegImplCode:
public void updatePloeg(PloegVO ploeg) throws BOException, AlreadyExistsException {
try {
DAOFactory.getPloegDAO().updatePloeg(ploeg);
// DAOFactory.getPloegRennerDAO().deletePloegRenners(ploeg.getId());
// createPloegRenners(ploeg);
}
catch (DAOException e) {
throw new BOException("DAOException in updatePloeg", e);
}
}
DAOFactoryCode:
public static PloegDAO getPloegDAO() {
return (PloegDAO) ApplicationContextFactory.getApplicationContext().getBean("hibernatePloegDAO");
}
testUpdatePloegCode:
public void testUpdatePloeg() throws BOException {
try {
PloegVO ploeg = PloegImpl.getInstance().findPloeg("e94ede61c3a2d49700bb869441efcc28");
if(ploeg != null) {
Logger.debug(this, ploeg.toString());
ploeg.setNaam("Team Rocket 2");
PloegImpl.getInstance().updatePloeg(ploeg);
}
}
catch (AlreadyExistsException e) {
throw new BOException("DAOException in testUpdatePloeg", e);
}
}
And to finish my entries in the
applicationContext.xmlCode:
<bean id="hibernatePloegDAOTarget" class="be.ngb.dao.HibernatePloegDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<bean id="hibernatePloegDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="hibernatePloegDAOTarget"/></property>
<property name="transactionAttributes">
<!-- define the transaction specs here -->
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
He tells something about 2 open session, but I can't find in the debug log where he could have opened those.
Has anyone encountered the same problem or do you know the solution to my problem? Feel free to respond ;) .
Any help would be appreciated.
Peter