Search in sources :

Example 21 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testImplicitJoiningWithExtraSynchronization.

@Test
public void testImplicitJoiningWithExtraSynchronization() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager entityManager = entityManagerFactory().createEntityManager();
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    entityManager.close();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 22 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testCloseAfterCommit.

@Test
public void testCloseAfterCommit() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager entityManager = entityManagerFactory().createEntityManager();
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 23 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testIsJoinedAfterMarkedForRollbackExplicit.

@Test
@TestForIssue(jiraKey = "HHH-10807")
public void testIsJoinedAfterMarkedForRollbackExplicit() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED);
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    entityManager.joinTransaction();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isJoinedToTransaction());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 24 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testIsJoinedAfterMarkedForRollbackImplict.

@Test
@TestForIssue(jiraKey = "HHH-10807")
public void testIsJoinedAfterMarkedForRollbackImplict() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager entityManager = entityManagerFactory().createEntityManager();
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isJoinedToTransaction());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 25 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class ByteCodeEnhancedImmutableReferenceCacheTest method testAssociatedWithMultiplePersistenceContexts.

@Test
@TestForIssue(jiraKey = "HHH-10795")
public void testAssociatedWithMultiplePersistenceContexts() {
    MyEnhancedReferenceData myReferenceData = new MyEnhancedReferenceData(1, "first item", "abc");
    MyEnhancedReferenceData myOtherReferenceData = new MyEnhancedReferenceData(2, "second item", "def");
    // save a reference in one session
    Session s1 = openSession();
    s1.beginTransaction();
    s1.save(myReferenceData);
    s1.save(myOtherReferenceData);
    s1.getTransaction().commit();
    s1.close();
    assertNotNull(myReferenceData.$$_hibernate_getEntityEntry());
    assertNotNull(myOtherReferenceData.$$_hibernate_getEntityEntry());
    // now associate myReferenceData with 2 sessions
    s1 = openSession();
    s1.beginTransaction();
    myReferenceData = s1.get(MyEnhancedReferenceData.class, myReferenceData.getId());
    myOtherReferenceData = s1.get(MyEnhancedReferenceData.class, myOtherReferenceData.getId());
    assertTrue(s1.contains(myReferenceData));
    assertTrue(s1.contains(myOtherReferenceData));
    // prev/next references should be null; entityEntry should be non-null;
    assertNull(myReferenceData.$$_hibernate_getPreviousManagedEntity());
    assertNull(myReferenceData.$$_hibernate_getNextManagedEntity());
    assertNull(myOtherReferenceData.$$_hibernate_getPreviousManagedEntity());
    assertNull(myOtherReferenceData.$$_hibernate_getNextManagedEntity());
    assertSame(myReferenceData.$$_hibernate_getEntityEntry(), ((SharedSessionContractImplementor) s1).getPersistenceContext().getEntry(myReferenceData));
    assertSame(myOtherReferenceData.$$_hibernate_getEntityEntry(), ((SharedSessionContractImplementor) s1).getPersistenceContext().getEntry(myOtherReferenceData));
    Session s2 = openSession();
    s2.beginTransaction();
    // s2 should contains no entities
    assertFalse(s2.contains(myReferenceData));
    assertFalse(s2.contains(myOtherReferenceData));
    assertNull(((SharedSessionContractImplementor) s2).getPersistenceContext().getEntry(myReferenceData));
    assertNull(((SharedSessionContractImplementor) s2).getPersistenceContext().getEntry(myOtherReferenceData));
    // evict should do nothing, since p is not associated with s2
    s2.evict(myReferenceData);
    s2.evict(myOtherReferenceData);
    assertSame(myReferenceData, s2.get(MyEnhancedReferenceData.class, myReferenceData.getId()));
    assertSame(myOtherReferenceData, s2.get(MyEnhancedReferenceData.class, myOtherReferenceData.getId()));
    assertTrue(s2.contains(myReferenceData));
    assertTrue(s2.contains(myOtherReferenceData));
    // still associated with s1
    assertTrue(s1.contains(myReferenceData));
    assertTrue(s1.contains(myOtherReferenceData));
    s2.evict(myReferenceData);
    s2.evict(myOtherReferenceData);
    assertFalse(s2.contains(myReferenceData));
    assertFalse(s2.contains(myOtherReferenceData));
    s2.getTransaction().commit();
    s2.close();
    // still associated with s1
    assertTrue(s1.contains(myReferenceData));
    assertTrue(s1.contains(myOtherReferenceData));
    s1.clear();
    assertFalse(s1.contains(myReferenceData));
    assertFalse(s1.contains(myOtherReferenceData));
    s1.close();
    // EntityEntry should still be set
    assertNotNull(myReferenceData.$$_hibernate_getEntityEntry());
    assertNotNull(myOtherReferenceData.$$_hibernate_getEntityEntry());
    // load them into 2 sessions
    s1 = openSession();
    s1.getTransaction().begin();
    assertSame(myReferenceData, s1.get(MyEnhancedReferenceData.class, myReferenceData.getId()));
    assertSame(myOtherReferenceData, s1.get(MyEnhancedReferenceData.class, myOtherReferenceData.getId()));
    s2 = openSession();
    s2.getTransaction().begin();
    assertSame(myReferenceData, s2.get(MyEnhancedReferenceData.class, myReferenceData.getId()));
    assertSame(myOtherReferenceData, s2.get(MyEnhancedReferenceData.class, myOtherReferenceData.getId()));
    assertEquals(Status.READ_ONLY, myReferenceData.$$_hibernate_getEntityEntry().getStatus());
    assertEquals(Status.READ_ONLY, myOtherReferenceData.$$_hibernate_getEntityEntry().getStatus());
    // delete myReferenceData from s1
    s1.delete(myReferenceData);
    assertEquals(Status.DELETED, myReferenceData.$$_hibernate_getEntityEntry().getStatus());
    assertEquals(Status.READ_ONLY, myOtherReferenceData.$$_hibernate_getEntityEntry().getStatus());
    // delete myOtherReferenceData from s2
    s2.delete(myOtherReferenceData);
    assertEquals(Status.DELETED, myReferenceData.$$_hibernate_getEntityEntry().getStatus());
    assertEquals(Status.DELETED, myOtherReferenceData.$$_hibernate_getEntityEntry().getStatus());
    s1.getTransaction().commit();
    s1.close();
    assertEquals(Status.GONE, myReferenceData.$$_hibernate_getEntityEntry().getStatus());
    assertEquals(Status.DELETED, myOtherReferenceData.$$_hibernate_getEntityEntry().getStatus());
    s2.getTransaction().commit();
    s2.close();
    assertEquals(Status.GONE, myReferenceData.$$_hibernate_getEntityEntry().getStatus());
    assertEquals(Status.GONE, myOtherReferenceData.$$_hibernate_getEntityEntry().getStatus());
}
Also used : SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)53 Test (org.junit.Test)16 CountDownLatch (java.util.concurrent.CountDownLatch)12 Session (org.hibernate.Session)10 EntityManager (javax.persistence.EntityManager)7 PutFromLoadValidator (org.hibernate.cache.infinispan.access.PutFromLoadValidator)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)7 Serializable (java.io.Serializable)6 AssertionFailedError (junit.framework.AssertionFailedError)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 HibernateException (org.hibernate.HibernateException)5 AssertionFailure (org.hibernate.AssertionFailure)4 TestForIssue (org.hibernate.testing.TestForIssue)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 List (java.util.List)3 TimeUnit (java.util.concurrent.TimeUnit)3 Transaction (org.hibernate.Transaction)3