HHH-7298 regression, org.hibernate.ejb.test.lock.LockTest

This commit is contained in:
Strong Liu 2012-05-31 18:58:00 +08:00
parent bf69f8a382
commit c0b4b7a577
7 changed files with 65 additions and 31 deletions

View File

@ -157,13 +157,9 @@ abstract class AbstractTransactSQLDialect extends Dialect {
return insertSQL + "\nselect @@identity"; return insertSQL + "\nselect @@identity";
} }
public String appendLockHint(LockMode mode, String tableName) { @Override
if ( mode.greaterThan( LockMode.READ ) ) { public String appendLockHint(LockOptions lockOptions, String tableName) {
return tableName + " holdlock"; return lockOptions.getLockMode().greaterThan( LockMode.READ ) ? tableName + " holdlock" : tableName;
}
else {
return tableName;
}
} }
public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map keyColumnNames) { public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map keyColumnNames) {

View File

@ -1281,8 +1281,23 @@ public abstract class Dialect implements ConversionContext {
* @param mode The lock mode to apply * @param mode The lock mode to apply
* @param tableName The name of the table to which to apply the lock hint. * @param tableName The name of the table to which to apply the lock hint.
* @return The table with any required lock hints. * @return The table with any required lock hints.
* @deprecated use {@code appendLockHint(LockOptions,String)} instead
*/ */
@Deprecated
public String appendLockHint(LockMode mode, String tableName) { public String appendLockHint(LockMode mode, String tableName) {
return appendLockHint( new LockOptions( mode ), tableName );
}
/**
* Some dialects support an alternative means to <tt>SELECT FOR UPDATE</tt>,
* whereby a "lock hint" is appends to the table name in the from clause.
* <p/>
* contributed by <a href="http://sourceforge.net/users/heschulz">Helge Schulz</a>
*
* @param lockOptions The lock options to apply
* @param tableName The name of the table to which to apply the lock hint.
* @return The table with any required lock hints.
*/
public String appendLockHint(LockOptions lockOptions, String tableName){
return tableName; return tableName;
} }

View File

@ -30,6 +30,7 @@ import java.util.regex.Pattern;
import org.hibernate.JDBCException; import org.hibernate.JDBCException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.QueryTimeoutException; import org.hibernate.QueryTimeoutException;
import org.hibernate.dialect.function.NoArgSQLFunction; import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.exception.LockTimeoutException; import org.hibernate.exception.LockTimeoutException;
@ -213,11 +214,25 @@ public class SQLServer2005Dialect extends SQLServerDialect {
} }
@Override // since SQLServer2005 the nowait hint is supported @Override // since SQLServer2005 the nowait hint is supported
public String appendLockHint(LockMode mode, String tableName) { public String appendLockHint(LockOptions lockOptions, String tableName) {
if ( mode == LockMode.UPGRADE_NOWAIT ) { if ( lockOptions.getLockMode() == LockMode.UPGRADE_NOWAIT ) {
return tableName + " with (updlock, rowlock, nowait)"; return tableName + " with (updlock, rowlock, nowait)";
} }
return super.appendLockHint( mode, tableName ); LockMode mode = lockOptions.getLockMode();
boolean isNoWait = lockOptions.getTimeOut() == LockOptions.NO_WAIT;
String noWaitStr = isNoWait? ", nowait" :"";
switch ( mode ) {
case UPGRADE_NOWAIT:
return tableName + " with (updlock, rowlock, nowait)";
case UPGRADE:
case PESSIMISTIC_WRITE:
case WRITE:
return tableName + " with (updlock, rowlock"+noWaitStr+" )";
case PESSIMISTIC_READ:
return tableName + " with (holdlock, rowlock"+noWaitStr+" )";
default:
return tableName;
}
} }
/** /**
@ -270,13 +285,18 @@ public class SQLServer2005Dialect extends SQLServerDialect {
@Override @Override
public JDBCException convert(SQLException sqlException, String message, String sql) { public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException ); final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if( "HY008".equals( sqlState )){ if( "HY008".equals( sqlState )){
throw new QueryTimeoutException( message, sqlException, sql ); throw new QueryTimeoutException( message, sqlException, sql );
} }
if (1222 == errorCode ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null; return null;
} }
}; };
} }
} }

View File

@ -25,6 +25,7 @@ package org.hibernate.dialect;
import java.sql.Types; import java.sql.Types;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.dialect.function.AnsiTrimEmulationFunction; import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
@ -120,17 +121,17 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
} }
@Override @Override
public String appendLockHint(LockMode mode, String tableName) { public String appendLockHint(LockOptions lockOptions, String tableName) {
if ( ( mode == LockMode.UPGRADE ) || LockMode mode = lockOptions.getLockMode();
( mode == LockMode.UPGRADE_NOWAIT ) || switch ( mode ) {
( mode == LockMode.PESSIMISTIC_WRITE ) || case UPGRADE:
( mode == LockMode.WRITE ) ) { case UPGRADE_NOWAIT:
case PESSIMISTIC_WRITE:
case WRITE:
return tableName + " with (updlock, rowlock)"; return tableName + " with (updlock, rowlock)";
} case PESSIMISTIC_READ:
else if ( mode == LockMode.PESSIMISTIC_READ ) {
return tableName + " with (holdlock, rowlock)"; return tableName + " with (holdlock, rowlock)";
} default:
else {
return tableName; return tableName;
} }
} }

View File

@ -72,31 +72,33 @@ public class SybaseASE157Dialect extends SybaseASE15Dialect {
} }
// support Lob Locator // support Lob Locator
@Override
public boolean supportsExpectedLobUsagePattern() { public boolean supportsExpectedLobUsagePattern() {
return true; return true;
} }
@Override
public boolean supportsLobValueChangePropogation() { public boolean supportsLobValueChangePropogation() {
return false; return false;
} }
// support 'select ... for update [of columns]' // support 'select ... for update [of columns]'
@Override
public boolean forUpdateOfColumns() { public boolean forUpdateOfColumns() {
return true; return true;
} }
@Override
public String getForUpdateString() { public String getForUpdateString() {
return " for update"; return " for update";
} }
@Override
public String getForUpdateString(String aliases) { public String getForUpdateString(String aliases) {
return getForUpdateString() + " of " + aliases; return getForUpdateString() + " of " + aliases;
} }
@Override
public String appendLockHint(LockMode mode, String tableName) { public String appendLockHint(LockOptions mode, String tableName) {
return tableName; return tableName;
} }
@Override
public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map keyColumnNames) { public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map keyColumnNames) {
return sql + new ForUpdateFragment( this, aliasedLockOptions, keyColumnNames ).toFragmentString(); return sql + new ForUpdateFragment( this, aliasedLockOptions, keyColumnNames ).toFragmentString();
} }

View File

@ -128,7 +128,7 @@ public abstract class AbstractEntityJoinWalker extends JoinWalker {
projection projection
) )
.setFromClause( .setFromClause(
getDialect().appendLockHint( lockOptions.getLockMode(), persister.fromTableFragment( alias ) ) + getDialect().appendLockHint( lockOptions, persister.fromTableFragment( alias ) ) +
persister.fromJoinFragment( alias, true, true ) persister.fromJoinFragment( alias, true, true )
) )
.setWhereClause( condition ) .setWhereClause( condition )

View File

@ -177,7 +177,7 @@ public class SimpleSelect {
} }
buf.append(" from ") buf.append(" from ")
.append( dialect.appendLockHint(lockOptions.getLockMode(), tableName) ); .append( dialect.appendLockHint(lockOptions, tableName) );
if ( whereTokens.size() > 0 ) { if ( whereTokens.size() > 0 ) {
buf.append(" where ") buf.append(" where ")