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();
}
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());
}
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());
}
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());
}
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());
}
Aggregations