Results 1 to 9 of 9

Thread: Hibernate exception - Illegal attempt to associate a collection with two open session

  1. #1

    Default Hibernate exception - Illegal attempt to associate a collection with two open session

    Hello

    I have the following error trace when i try to perform an update.
    All my beans are being read in from an xml file and updates work perfectly for other dao methods.

    org.springframework.orm.hibernate.HibernateSystemE xception: 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.s etCurrentSession(PersistentCollection.java:257)
    at net.sf.hibernate.impl.OnUpdateVisitor.processColle ction(OnUpdateVisitor.java:38)
    at net.sf.hibernate.impl.AbstractVisitor.processValue (AbstractVisitor.java:69)
    at net.sf.hibernate.impl.AbstractVisitor.processValue s(AbstractVisitor.java:36)
    at net.sf.hibernate.impl.AbstractVisitor.process(Abst ractVisitor.java:91)
    at net.sf.hibernate.impl.SessionImpl.doUpdateMutable( SessionImpl.java:1465)
    at net.sf.hibernate.impl.SessionImpl.doUpdate(Session Impl.java:1479)
    at net.sf.hibernate.impl.SessionImpl.update(SessionIm pl.java:1364)
    at org.springframework.orm.hibernate.HibernateTemplat e$11.doInHibernate(HibernateTemplate.java:530)
    at org.springframework.orm.hibernate.HibernateTemplat e.execute(HibernateTemplate.java:317)
    at org.springframework.orm.hibernate.HibernateTemplat e.update(HibernateTemplate.java:527)
    at org.springframework.orm.hibernate.HibernateTemplat e.update(HibernateTemplate.java:523)
    at com.fmr.eca.dao.hibernate.ExpenseTypeDaoHibernate. modifyExpenseType(ExpenseTypeDaoHibernate.java:141 )
    at junit.com.fmr.eca.dao.hibernate.TestExpenseTypeDao Hibernate.testModifyExpenseTypes(TestExpenseTypeDa oHibernate.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:79)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:41)
    at java.lang.reflect.Method.invoke(Method.java:386)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:392)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:276)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:167)




    Why would this error be happening?
    Any help greatly appreciated

    Regards
    Damien

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Why would this error be happening?
    You must be using a collection associated with one session in another session. Would have to look at the code to determine where.

    You find a better explanation here: http://forum.hibernate.org/

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Btw, try searching the forum - you should find plenty of posts on this topic.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Jul 2007
    Posts
    2

    Default Found no answer in forum

    I have searched forum for this problem. However, this topic is the only hit.

    It is too strange. When I run
    IdentityService service = new IdentityServiceImpl();
    service.update();
    Everything is okay.

    When I run
    IdentityService service = Context.getBean(...);
    service.update();

    This exception will be throwed.

    In update(), I call dao loading a pojo and updating it.

    It obviously due to SessionFactoryUtils returns two sessions: loading is a session, updationg is another session. Why?

  5. #5
    Join Date
    Mar 2007
    Posts
    515

    Default

    Can you post more details ? (actual test code, config file etc)

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    As Andrei said posting more details would help. One point however is that if you are creating your service using the new operator, it's not going to be Spring managed and hence you are going to have completely different behaviour/problems.
    Last edited by karldmoore; Aug 29th, 2007 at 11:00 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Jul 2007
    Posts
    2

    Default I have resolved it

    ===Illegal attempt to associate a collection with two open session===
    ====Exception Message====
    org.springframework.orm.hibernate3.HibernateSystem Exception: Illegal attempt to associate a collection with two open sessions;

    nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

    ====Cause====
    The program started two or more transactions while we expected only one. It is a fact caused by incorrect transaction AOP configuration in Spring context.xml

    Our default transaction AOP config is:
    <tx:advice id="txAdvice">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="move*" propagation="REQUIRED"/>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
    </tx:advice>

    This configuration means: all methods start with "add", "delete", "update", "move", "save" require transaction. all other methods support transaction.

    for example, method login() calls load() and update(). both login() and load() do not require transaction, but support it. update() require transaction.

    However, Both load() and update() will start new transaction. But we expected load() is out of transaction of at the same transaction with update().

    ====Fixing====
    Declare the new method as REQUIRED.
    # declare load*, find* and all other persistence query method as REQUIRED.
    # declare All APIs as REQUIRED, as long as it called persistence CUD methods.

  8. #8
    Join Date
    Mar 2007
    Posts
    515

    Default

    PROPAGATION_SUPPORTS doesn't start a transaction, only REQUIRED and REQUIRES_NEW do that.
    A Hibernate session doesn't necessarily mean that you'll have a transaction.
    Juergen's post from this thread explains better the behavior of PROPAGATION_SUPPORTS.

  9. #9
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    I'm not sure I understand the solution either. I'd be quite interested to see the code and configuration that worked and didn't (in [code] [ /code] tags) just to understand what was going on.
    Last edited by karldmoore; Aug 29th, 2007 at 11:00 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •