Announcement Announcement Module
Collapse
No announcement yet.
Illegal attempt to associate a collection with two open sessions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Illegal attempt to associate a collection with two open sessions

    To all Spring/Hibernate Gurus,

    I am using the following code(in bold), but I get an exception when i try to delete the data from the sourceDao within the transaction(exception is given below). I even tried copying data to a different list before passing it to the delete method, but it still gives the same exception.

    pData = sourceDao.getSourceData(startDate, endDate);

    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.P ROPAGATION_REQUIRED);
    TransactionStatus status = this.secTxManager.getTransaction(def);
    try{

    pageCount = secArchiveDao.archive(pData);
    sourceDao.deleteSourceData(pData);
    secTxManager.commit(status);
    success = true;

    } catch (Exception ex)
    {
    secTxManager.rollback(status);

    }




    Exception:
    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
    org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
    at org.hibernate.collection.AbstractPersistentCollect ion.setCurrentSession(AbstractPersistentCollection .java:237)
    at org.hibernate.event.def.OnUpdateVisitor.processCol lection(OnUpdateVisitor.java:41)
    at org.hibernate.event.def.AbstractVisitor.processVal ue(AbstractVisitor.java:104)
    at org.hibernate.event.def.AbstractVisitor.processVal ue(AbstractVisitor.java:64)
    at org.hibernate.event.def.AbstractVisitor.processEnt ityPropertyValues(AbstractVisitor.java:58)
    at org.hibernate.event.def.AbstractVisitor.process(Ab stractVisitor.java:129)
    at org.hibernate.event.def.DefaultDeleteEventListener .onDelete(DefaultDeleteEventListener.java:74)
    at org.hibernate.impl.SessionImpl.delete(SessionImpl. java:579)
    at org.springframework.orm.hibernate3.HibernateTempla te$26.doInHibernate(HibernateTemplate.java:703)
    at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:312)
    at org.springframework.orm.hibernate3.HibernateTempla te.deleteAll(HibernateTemplate.java:699)
    at org.tiaa.secarchive.dao.impl.SecProductionCltDAOIm pl.deleteSourceData(SecProductionCltDAOImpl.java:7 6)
    at org.tiaa.secarchive.delegate.impl.SecArchiveDelega teImpl.archive(SecArchiveDelegateImpl.java:86)
    at org.tiaa.secarchive.schedule.impl.JobImpl.archive( JobImpl.java:328)
    at org.tiaa.secarchive.schedule.impl.JobImpl.execute( JobImpl.java:210)
    at org.tiaa.secarchive.schedule.impl.DaemonImpl.run(D aemonImpl.java:239)
    at java.lang.Thread.run(Thread.java:534)
    org.tiaa.j2eeinfra.exception.TiaaRuntimeException: There are no open seeds in the database. New Seeds need to be created !
    at org.tiaa.secarchive.schedule.impl.JobImpl.checkSee dsAvailability(JobImpl.java:415)
    at org.tiaa.secarchive.schedule.impl.JobImpl.execute( JobImpl.java:80)
    at org.tiaa.secarchive.schedule.impl.DaemonImpl.run(D aemonImpl.java:239)
    at java.lang.Thread.run(Thread.java:534)

    Any help is greatly appreciated:
    Thanks
    Aadil

  • #2
    The retrieval of your data occurs in the context of an open transaction (i.e. hibernate session). This session is not yet closed when you try to store the data in another session (that is, the programmatically opened transaction).

    Try declaring the first transaction (for reading) as "readOnly" (unless you perform writing also). If that does not help, you have to manually detach the retrieved data from the first session before storing it in another session.

    If the first session is also controlled programmatically, you might consider closing it before opening the second one.

    Regards,
    Andreas

    Comment


    • #3
      Need to write to one databae and delete from another!!

      I need to read data from one hibernate session and write it to another and then go back and delete the data from the first session. The writing of data and deleting should be in the same session. If i close the session for the pageCount = secArchiveDao.archive(pData); call , doesnt that mean commit (even before the delete call is made)?

      How do I manually detach the retrieved data from the first session? I tried creating a new List , copying the data to it and setting the previous list to null but somehow still it throws the same exception.

      More help appreciated !!

      Comment


      • #4
        Originally posted by adil
        I need to read data from one hibernate session and write it to another and then go back and delete the data from the first session. The writing of data and deleting should be in the same session. If i close the session for the pageCount = secArchiveDao.archive(pData); call , doesnt that mean commit (even before the delete call is made)?
        Yes that would do a commit and is therefore not appropriate in this situation.

        Originally posted by adil
        How do I manually detach the retrieved data from the first session? I tried creating a new List , copying the data to it and setting the previous list to null but somehow still it throws the same exception.
        You can do an evict() on the first session to remove an object from its cache. Besides that, just moving elements around is not effective, instead you could try cloning the instances (if supported). In that case you should have deep copies that could be used in another session.

        Regards,
        Andreas

        Comment

        Working...
        X