use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SessionImpl method find.
@Override
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
LockOptions lockOptions = null;
try {
if (properties != null && !properties.isEmpty()) {
getLoadQueryInfluencers().setFetchGraph((EntityGraph) properties.get(QueryHints.HINT_FETCHGRAPH));
getLoadQueryInfluencers().setLoadGraph((EntityGraph) properties.get(QueryHints.HINT_LOADGRAPH));
}
final IdentifierLoadAccess<T> loadAccess = byId(entityClass);
loadAccess.with(determineAppropriateLocalCacheMode(properties));
if (lockModeType != null) {
if (!LockModeType.NONE.equals(lockModeType)) {
checkTransactionNeeded();
}
lockOptions = buildLockOptions(lockModeType, properties);
loadAccess.with(lockOptions);
}
return loadAccess.load((Serializable) primaryKey);
} catch (EntityNotFoundException ignored) {
// which find() should not throw. Find() should return null if the entity was not found.
if (log.isDebugEnabled()) {
String entityName = entityClass != null ? entityClass.getName() : null;
String identifierValue = primaryKey != null ? primaryKey.toString() : null;
log.ignoringEntityNotFound(entityName, identifierValue);
}
return null;
} catch (ObjectDeletedException e) {
//the spec is silent about people doing remove() find() on the same PC
return null;
} catch (ObjectNotFoundException e) {
//should not happen on the entity itself with get
throw new IllegalArgumentException(e.getMessage(), e);
} catch (MappingException | TypeMismatchException | ClassCastException e) {
throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
} catch (RuntimeException e) {
throw exceptionConverter.convert(e, lockOptions);
} finally {
getLoadQueryInfluencers().setFetchGraph(null);
getLoadQueryInfluencers().setLoadGraph(null);
}
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SessionImpl method refresh.
@Override
public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
final CacheMode previousCacheMode = getCacheMode();
final CacheMode refreshCacheMode = determineAppropriateLocalCacheMode(properties);
LockOptions lockOptions = null;
try {
setCacheMode(refreshCacheMode);
if (!contains(entity)) {
throw exceptionConverter.convert(new IllegalArgumentException("Entity not managed"));
}
if (lockModeType != null) {
if (!LockModeType.NONE.equals(lockModeType)) {
checkTransactionNeeded();
}
lockOptions = buildLockOptions(lockModeType, properties);
refresh(entity, lockOptions);
} else {
refresh(entity);
}
} catch (MappingException e) {
throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
} catch (RuntimeException e) {
throw exceptionConverter.convert(e, lockOptions);
} finally {
setCacheMode(previousCacheMode);
}
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SessionImpl method lock.
@Override
public void lock(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
checkTransactionNeeded();
if (!contains(entity)) {
throw new IllegalArgumentException("entity not in the persistence context");
}
final LockOptions lockOptions = buildLockOptions(lockModeType, properties);
try {
buildLockRequest(lockOptions).lock(entity);
} catch (RuntimeException e) {
throw exceptionConverter.convert(e, lockOptions);
}
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class CriteriaLoader method applyLocks.
@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
final LockOptions lockOptions = parameters.getLockOptions();
if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && (lockOptions.getAliasLockCount() == 0 || (lockOptions.getAliasLockCount() == 1 && lockOptions.getAliasSpecificLockMode("this_") == LockMode.NONE)))) {
return sql;
}
if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
final LockMode lockMode = determineFollowOnLockMode(lockOptions);
if (lockMode != LockMode.UPGRADE_SKIPLOCKED) {
// Dialect prefers to perform locking in a separate step
LOG.usingFollowOnLocking();
final LockOptions lockOptionsToUse = new LockOptions(lockMode);
lockOptionsToUse.setTimeOut(lockOptions.getTimeOut());
lockOptionsToUse.setScope(lockOptions.getScope());
afterLoadActions.add(new AfterLoadAction() {
@Override
public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
((Session) session).buildLockRequest(lockOptionsToUse).lock(persister.getEntityName(), entity);
}
});
parameters.setLockOptions(new LockOptions());
return sql;
}
}
final LockOptions locks = new LockOptions(lockOptions.getLockMode());
locks.setScope(lockOptions.getScope());
locks.setTimeOut(lockOptions.getTimeOut());
final Map<String, String[]> keyColumnNames = dialect.forUpdateOfColumns() ? new HashMap() : null;
final String[] drivingSqlAliases = getAliases();
for (int i = 0; i < drivingSqlAliases.length; i++) {
final LockMode lockMode = lockOptions.getAliasSpecificLockMode(drivingSqlAliases[i]);
if (lockMode != null) {
final Lockable drivingPersister = (Lockable) getEntityPersisters()[i];
final String rootSqlAlias = drivingPersister.getRootTableAlias(drivingSqlAliases[i]);
locks.setAliasSpecificLockMode(rootSqlAlias, lockMode);
if (keyColumnNames != null) {
keyColumnNames.put(rootSqlAlias, drivingPersister.getRootTableIdentifierColumnNames());
}
}
}
return dialect.applyLocksToSql(sql, locks, keyColumnNames);
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class OracleFollowOnLockingTest method testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking.
@Test
public void testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products = session.createQuery("select p from Product p order by p.id", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE)).setMaxResults(10).getResultList();
assertEquals(10, products.size());
assertEquals(11, sqlStatementInterceptor.getSqlQueries().size());
session.getTransaction().commit();
session.close();
}
Aggregations