SelectLockingStrategypublic class SelectLockingStrategy extends Object implements LockingStrategyA locking strategy where the locks are obtained through select statements.
For non-read locks, this is achieved through the Dialect's specific
SELECT ... FOR UPDATE syntax. |
Fields Summary |
---|
private final org.hibernate.persister.entity.Lockable | lockable | private final org.hibernate.LockMode | lockMode | private final String | sql |
Methods Summary |
---|
protected java.lang.String | generateLockString()
SessionFactoryImplementor factory = lockable.getFactory();
SimpleSelect select = new SimpleSelect( factory.getDialect() )
.setLockMode( lockMode )
.setTableName( lockable.getRootTableName() )
.addColumn( lockable.getRootTableIdentifierColumnNames()[0] )
.addCondition( lockable.getRootTableIdentifierColumnNames(), "=?" );
if ( lockable.isVersioned() ) {
select.addCondition( lockable.getVersionColumnName(), "=?" );
}
if ( factory.getSettings().isCommentsEnabled() ) {
select.setComment( lockMode + " lock " + lockable.getEntityName() );
}
return select.toStatementString();
| protected org.hibernate.LockMode | getLockMode()
return lockMode;
| public void | lock(java.io.Serializable id, java.lang.Object version, java.lang.Object object, org.hibernate.engine.SessionImplementor session)
SessionFactoryImplementor factory = session.getFactory();
try {
PreparedStatement st = session.getBatcher().prepareSelectStatement( sql );
try {
lockable.getIdentifierType().nullSafeSet( st, id, 1, session );
if ( lockable.isVersioned() ) {
lockable.getVersionType().nullSafeSet(
st,
version,
lockable.getIdentifierType().getColumnSpan( factory ) + 1,
session
);
}
ResultSet rs = st.executeQuery();
try {
if ( !rs.next() ) {
if ( factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor()
.optimisticFailure( lockable.getEntityName() );
}
throw new StaleObjectStateException( lockable.getEntityName(), id );
}
}
finally {
rs.close();
}
}
finally {
session.getBatcher().closeStatement( st );
}
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
}
|
|