Search in sources :

Example 16 with SharedSessionContractImplementor

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

the class LocalTemporaryTableBulkIdStrategy method buildDeleteHandler.

@Override
public DeleteHandler buildDeleteHandler(SessionFactoryImplementor factory, HqlSqlWalker walker) {
    final DeleteStatement updateStatement = (DeleteStatement) walker.getAST();
    final FromElement fromElement = updateStatement.getFromClause().getFromElement();
    final Queryable targetedPersister = fromElement.getQueryable();
    final IdTableInfoImpl tableInfo = getIdTableInfo(targetedPersister);
    return new TableBasedDeleteHandlerImpl(factory, walker, tableInfo) {

        @Override
        protected void prepareForUse(Queryable persister, SharedSessionContractImplementor session) {
            Helper.INSTANCE.createTempTable(tableInfo, ddlTransactionHandling, session);
        }

        @Override
        protected void releaseFromUse(Queryable persister, SharedSessionContractImplementor session) {
            Helper.INSTANCE.releaseTempTable(tableInfo, afterUseAction, ddlTransactionHandling, session);
        }
    };
}
Also used : TableBasedDeleteHandlerImpl(org.hibernate.hql.spi.id.TableBasedDeleteHandlerImpl) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) Queryable(org.hibernate.persister.entity.Queryable) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) DeleteStatement(org.hibernate.hql.internal.ast.tree.DeleteStatement)

Example 17 with SharedSessionContractImplementor

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

the class Loader method shouldUseFollowOnLocking.

protected boolean shouldUseFollowOnLocking(QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) {
    if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
        // currently only one lock mode is allowed in follow-on locking
        final LockMode lockMode = determineFollowOnLockMode(parameters.getLockOptions());
        final LockOptions lockOptions = new LockOptions(lockMode);
        if (lockOptions.getLockMode() != LockMode.UPGRADE_SKIPLOCKED) {
            if (lockOptions.getLockMode() != LockMode.NONE) {
                LOG.usingFollowOnLocking();
            }
            lockOptions.setTimeOut(parameters.getLockOptions().getTimeOut());
            lockOptions.setScope(parameters.getLockOptions().getScope());
            afterLoadActions.add(new AfterLoadAction() {

                @Override
                public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
                    ((Session) session).buildLockRequest(lockOptions).lock(persister.getEntityName(), entity);
                }
            });
            parameters.setLockOptions(new LockOptions());
            return true;
        }
    }
    return false;
}
Also used : UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) LockOptions(org.hibernate.LockOptions) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) LockMode(org.hibernate.LockMode) Session(org.hibernate.Session)

Example 18 with SharedSessionContractImplementor

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

the class AbstractLazyInitializer method permissiveInitialization.

protected void permissiveInitialization() {
    if (session == null) {
        //we have a detached collection thats set to null, reattach
        if (sessionFactoryUuid == null) {
            throw new LazyInitializationException("could not initialize proxy - no Session");
        }
        try {
            SessionFactoryImplementor sf = (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.getSessionFactory(sessionFactoryUuid);
            SharedSessionContractImplementor session = (SharedSessionContractImplementor) sf.openSession();
            session.getPersistenceContext().setDefaultReadOnly(true);
            session.setFlushMode(FlushMode.MANUAL);
            boolean 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 {
                target = session.immediateLoad(entityName, id);
                initialized = true;
                checkTargetState(session);
            } finally {
                // make sure the just opened temp session gets closed!
                try {
                    if (!isJTA) {
                        session.getTransaction().commit();
                    }
                    session.close();
                } catch (Exception e) {
                    log.warn("Unable to close temporary session used to load lazy proxy associated to no session");
                }
            }
        } catch (Exception e) {
            log.error("Initialization failure", e);
            throw new LazyInitializationException(e.getMessage());
        }
    } else if (session.isOpen() && session.isConnected()) {
        target = session.immediateLoad(entityName, id);
        initialized = true;
        checkTargetState(session);
    } else {
        throw new LazyInitializationException("could not initialize proxy - Session was closed or disced");
    }
}
Also used : LazyInitializationException(org.hibernate.LazyInitializationException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) TransientObjectException(org.hibernate.TransientObjectException) LazyInitializationException(org.hibernate.LazyInitializationException) HibernateException(org.hibernate.HibernateException) SessionException(org.hibernate.SessionException)

Example 19 with SharedSessionContractImplementor

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

the class BasicLazyInitializer method getReplacement.

private Object getReplacement() {
    final SharedSessionContractImplementor session = getSession();
    if (isUninitialized() && session != null && session.isOpen()) {
        final EntityKey key = session.generateEntityKey(getIdentifier(), session.getFactory().getMetamodel().entityPersister(getEntityName()));
        final Object entity = session.getPersistenceContext().getEntity(key);
        if (entity != null) {
            setImplementation(entity);
        }
    }
    if (isUninitialized()) {
        if (replacement == null) {
            replacement = serializableProxy();
        }
        return replacement;
    } else {
        return getTarget();
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) MarkerObject(org.hibernate.internal.util.MarkerObject)

Example 20 with SharedSessionContractImplementor

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

the class TransactionJoinHandlingChecker method validateExplicitJoiningHandling.

static void validateExplicitJoiningHandling(EntityManager entityManager) throws Exception {
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertFalse(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    session.getFlushMode();
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertFalse(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    session.getFlushMode();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertFalse(transactionCoordinator.isJoined());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    entityManager.joinTransaction();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    assertTrue(transactionCoordinator.isJtaTransactionCurrentlyActive());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    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 : SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)

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