use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class TableGenerator method buildSelectQuery.
@SuppressWarnings("unchecked")
protected String buildSelectQuery(Dialect dialect) {
final String alias = "tbl";
final String query = "select " + StringHelper.qualify(alias, valueColumnName) + " from " + renderedTableName + ' ' + alias + " where " + StringHelper.qualify(alias, segmentColumnName) + "=?";
final LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE);
lockOptions.setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_WRITE);
final Map updateTargetColumnsMap = Collections.singletonMap(alias, new String[] { valueColumnName });
return dialect.applyLocksToSql(query, lockOptions, updateTargetColumnsMap);
}
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);
}
}
Aggregations