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