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