Search in sources :

Example 31 with SharedSessionContractImplementor

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

the class OtherEntityEntryContextTestTask method execute.

public void execute() {
    Session s1 = getFactory().openSession();
    s1.beginTransaction();
    Parent p = (Parent) s1.get(Parent.class, 1);
    assertTrue(ManagedEntity.class.isInstance(p));
    p.setName("second");
    assertTrue(s1.contains(p));
    // open another session and evict p from the new session
    Session s2 = getFactory().openSession();
    s2.beginTransaction();
    // s2 should contains no entities
    assertFalse(s2.contains(p));
    // evict should do nothing, since p is not associated with s2
    s2.evict(p);
    assertFalse(s2.contains(p));
    assertNull(((SharedSessionContractImplementor) s2).getPersistenceContext().getEntry(p));
    try {
        s2.update(p);
        fail("should have failed because p is already associated with a PersistenceContext that is still open.");
    } catch (HibernateException expected) {
        // expected
        s2.getTransaction().rollback();
        s2.close();
    }
    s1.getTransaction().commit();
    s1.close();
    p.setName("third");
    s1 = getFactory().openSession();
    s1.getTransaction().begin();
    s1.update(p);
    assertTrue(s1.contains(p));
    s1.evict(p);
    assertFalse(s1.contains(p));
    p = s1.get(Parent.class, p.getId());
    assertEquals("second", p.getName());
    s1.getTransaction().commit();
    s1.close();
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity) HibernateException(org.hibernate.HibernateException) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Session(org.hibernate.Session)

Example 32 with SharedSessionContractImplementor

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

the class Helper method performWork.

public <T> T performWork(LazyInitializationWork<T> lazyInitializationWork) {
    SharedSessionContractImplementor session = consumer.getLinkedSession();
    boolean isTempSession = false;
    boolean isJta = false;
    // first figure out which Session to use
    if (session == null) {
        if (consumer.allowLoadOutsideTransaction()) {
            session = openTemporarySessionForLoading(lazyInitializationWork);
            isTempSession = true;
        } else {
            throwLazyInitializationException(Cause.NO_SESSION, lazyInitializationWork);
        }
    } else if (!session.isOpen()) {
        if (consumer.allowLoadOutsideTransaction()) {
            session = openTemporarySessionForLoading(lazyInitializationWork);
            isTempSession = true;
        } else {
            throwLazyInitializationException(Cause.CLOSED_SESSION, lazyInitializationWork);
        }
    } else if (!session.isConnected()) {
        if (consumer.allowLoadOutsideTransaction()) {
            session = openTemporarySessionForLoading(lazyInitializationWork);
            isTempSession = true;
        } else {
            throwLazyInitializationException(Cause.DISCONNECTED_SESSION, lazyInitializationWork);
        }
    }
    // If we are using a temporary Session, begin a transaction if necessary
    if (isTempSession) {
        isJta = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta();
        if (!isJta) {
            // Explicitly handle the transactions only if we're not in
            // a JTA environment.  A lazy loading temporary session can
            // be created even if a current session and transaction are
            // open (ex: session.clear() was used).  We must prevent
            // multiple transactions.
            session.beginTransaction();
        }
    }
    try {
        // do the actual work
        return lazyInitializationWork.doWork(session, isTempSession);
    } finally {
        if (isTempSession) {
            try {
                // Commit the JDBC transaction is we started one.
                if (!isJta) {
                    session.getTransaction().commit();
                }
            } catch (Exception e) {
                log.warn("Unable to commit JDBC transaction on temporary session used to load lazy " + "collection associated to no session");
            }
            // Close the just opened temp Session
            try {
                session.close();
            } catch (Exception e) {
                log.warn("Unable to close temporary session used to load lazy collection associated to no session");
            }
        }
    }
}
Also used : SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) LazyInitializationException(org.hibernate.LazyInitializationException)

Example 33 with SharedSessionContractImplementor

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

the class XmlTest method testXmlMappingWithCacheable.

@Test
public void testXmlMappingWithCacheable() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    SharedSessionContractImplementor session = em.unwrap(SharedSessionContractImplementor.class);
    EntityPersister entityPersister = session.getFactory().getEntityPersister(Lighter.class.getName());
    Assert.assertTrue(entityPersister.hasCache());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Test(org.junit.Test)

Example 34 with SharedSessionContractImplementor

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

the class SynchronizationTypeTest method testImplicitJoining.

@Test
public void testImplicitJoining() throws Exception {
    // here the transaction is started beforeQuery the EM is opened.  Because the SynchronizationType is UNSYNCHRONIZED
    // though, it should not auto join the transaction
    assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED, null);
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertFalse("EM was auto joined on creation", transactionCoordinator.isSynchronizationRegistered());
    assertTrue("EM was auto joined on creation", transactionCoordinator.isActive());
    assertFalse("EM was auto joined on creation", transactionCoordinator.isJoined());
    session.getFlushMode();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertFalse(transactionCoordinator.isJoined());
    entityManager.joinTransaction();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    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 35 with SharedSessionContractImplementor

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

the class TransactionJoiningTest method testImplicitJoining.

@Test
public void testImplicitJoining() throws Exception {
    // here the transaction is started beforeQuery the EM is opened...
    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());
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    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)

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