Uploaded image for project: 'Hibernate ORM'
  1. Hibernate ORM
  2. HHH-11580

EnversPreCollectionRemoveEventListener fails because EntityManager is closed when using JPA + JTA + Envers

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.2.9
    • Fix Version/s: 5.2.10
    • Component/s: hibernate-envers
    • Labels:
      None

      Description

      My environment:
      Using spring with JPA + JTA and Hibernate 5.2.9.Final + Envers

      This issue is related to HHH-11570 Closed

      When deleting an entity that has a one-to-many relationship to another one (without cascade delete), EnversPreCollectionRemoveEventListener might fail since entity manager is already closed.

      Exception in thread "main" org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException: The transaction was set to rollback only
      	at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1026)
      	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
      	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
      	at com.myapp.services.MyService.reproduceIssue(MyService.java:51)
      	at com.myapp.App.main(App.java:12)
      Caused by: javax.transaction.RollbackException: The transaction was set to rollback only
      	at com.atomikos.icatch.jta.TransactionImp.rethrowAsJtaRollbackException(TransactionImp.java:66)
      	at com.atomikos.icatch.jta.TransactionImp.commit(TransactionImp.java:207)
      	at com.atomikos.icatch.jta.TransactionManagerImp.commit(TransactionManagerImp.java:433)
      	at com.atomikos.icatch.jta.J2eeUserTransaction.commit(J2eeUserTransaction.java:94)
      	at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1023)
      	... 4 more
      Caused by: javax.persistence.PersistenceException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myapp.entities.MyEntity.myOtherEntities, could not initialize proxy - the owning Session was closed
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
      	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
      	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1434)
      	at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:484)
      	at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3190)
      	at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2404)
      	at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
      	at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.beforeCompletion(JtaTransactionCoordinatorImpl.java:320)
      	at org.hibernate.resource.transaction.backend.jta.internal.synchronization.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:47)
      	at org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:37)
      	at com.atomikos.icatch.jta.Sync2Sync.beforeCompletion(Sync2Sync.java:50)
      	at com.atomikos.icatch.imp.TransactionStateHandler.notifyBeforeCompletion(TransactionStateHandler.java:244)
      	at com.atomikos.icatch.imp.TransactionStateHandler.commit(TransactionStateHandler.java:220)
      	at com.atomikos.icatch.imp.CompositeTransactionImp.doCommit(CompositeTransactionImp.java:288)
      	at com.atomikos.icatch.imp.CompositeTransactionImp.commit(CompositeTransactionImp.java:337)
      	at com.atomikos.icatch.jta.TransactionImp.commit(TransactionImp.java:191)
      	... 7 more
      Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myapp.entities.MyEntity.myOtherEntities, could not initialize proxy - the owning Session was closed
      	at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
      	at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:209)
      	at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
      	at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:729)
      	at org.hibernate.envers.event.spi.BaseEnversCollectionEventListener.initializeCollection(BaseEnversCollectionEventListener.java:136)
      	at org.hibernate.envers.event.spi.EnversPreCollectionRemoveEventListenerImpl.onPreRemoveCollection(EnversPreCollectionRemoveEventListenerImpl.java:40)
      	at org.hibernate.action.internal.CollectionRemoveAction.preRemove(CollectionRemoveAction.java:124)
      	at org.hibernate.action.internal.CollectionRemoveAction.execute(CollectionRemoveAction.java:89)
      	at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:586)
      	at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:460)
      	at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
      	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
      	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1428)
      	... 20 more
      

      Test case is attached: MyApp.zip
      Main class: com.myapp.App

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: