HHH-17506 Return exceptions instead of throwing them in SQLExceptionConversionDelegate

This commit is contained in:
Christian Beikov 2023-12-20 17:45:28 +01:00
parent 6168582103
commit c931c86896
14 changed files with 70 additions and 37 deletions

View File

@ -892,11 +892,10 @@ public class DB2LegacyDialect extends Dialect {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( -952 == errorCode && "57014".equals( sqlState ) ) {
throw new LockTimeoutException( message, sqlException, sql );
switch ( errorCode ) {
case -952:
return new LockTimeoutException( message, sqlException, sql );
}
return null;
};

View File

@ -716,7 +716,7 @@ public class DerbyLegacyDialect extends Dialect {
switch ( sqlState ) {
case "40XL1":
case "40XL2":
throw new LockTimeoutException( message, sqlException, sql );
return new LockTimeoutException( message, sqlException, sql );
}
}
return null;

View File

@ -1015,13 +1015,13 @@ public class OracleLegacyDialect extends Dialect {
case 30006:
// ORA-30006: resource busy; acquire with WAIT timeout expired
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
case 54:
// ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
case 4021:
// ORA-04021 timeout occurred while waiting to lock object
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
// deadlocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1036,7 +1036,7 @@ public class OracleLegacyDialect extends Dialect {
case 1013:
// ORA-01013: user requested cancel of current operation
throw new QueryTimeoutException( message, sqlException, sql );
return new QueryTimeoutException( message, sqlException, sql );
// data integrity violation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -731,14 +731,25 @@ public class SQLServerLegacyDialect extends AbstractTransactSQLDialect {
}
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( "HY008".equals( sqlState ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
return new QueryTimeoutException( message, sqlException, sql );
}
if ( 1222 == errorCode ) {
throw new LockTimeoutException( message, sqlException, sql );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( errorCode ) {
case 1222:
return new LockTimeoutException( message, sqlException, sql );
case 2627:
case 2601:
return new ConstraintViolationException(
message,
sqlException,
sql,
getViolatedConstraintNameExtractor().extractConstraintName( sqlException )
);
default:
return null;
}
return null;
};
}
@ -1105,4 +1116,14 @@ public class SQLServerLegacyDialect extends AbstractTransactSQLDialect {
// public String getEnableConstraintStatement(String tableName, String name) {
// return "alter table " + tableName + " with check check constraint " + name;
// }
@Override
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}
@Override
public boolean supportsFromClauseInUpdate() {
return true;
}
}

View File

@ -667,7 +667,7 @@ public class SybaseASELegacyDialect extends SybaseLegacyDialect {
switch ( sqlState ) {
case "JZ0TO":
case "JZ006":
throw new LockTimeoutException( message, sqlException, sql );
return new LockTimeoutException( message, sqlException, sql );
case "S1000":
switch ( errorCode ) {
case 515:

View File

@ -515,7 +515,7 @@ public abstract class AbstractHANADialect extends Dialect {
// 262 - Invalid query name
// 263 - Invalid alias name
if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) {
throw new SQLGrammarException( message, sqlException, sql );
return new SQLGrammarException( message, sqlException, sql );
}
// 257 - Cannot insert NULL or update to NULL

View File

@ -976,11 +976,10 @@ public class DB2Dialect extends Dialect {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( -952 == errorCode && "57014".equals( sqlState ) ) {
throw new LockTimeoutException( message, sqlException, sql );
switch ( errorCode ) {
case -952:
return new LockTimeoutException( message, sqlException, sql );
}
return null;
};

View File

@ -700,7 +700,7 @@ public class DerbyDialect extends Dialect {
switch ( sqlState ) {
case "40XL1":
case "40XL2":
throw new LockTimeoutException( message, sqlException, sql );
return new LockTimeoutException( message, sqlException, sql );
}
}
return null;

View File

@ -1055,13 +1055,13 @@ public class OracleDialect extends Dialect {
case 30006:
// ORA-30006: resource busy; acquire with WAIT timeout expired
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
case 54:
// ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
case 4021:
// ORA-04021 timeout occurred while waiting to lock object
throw new LockTimeoutException(message, sqlException, sql);
return new LockTimeoutException(message, sqlException, sql);
// deadlocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1076,7 +1076,7 @@ public class OracleDialect extends Dialect {
case 1013:
// ORA-01013: user requested cancel of current operation
throw new QueryTimeoutException( message, sqlException, sql );
return new QueryTimeoutException( message, sqlException, sql );
// data integrity violation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -704,16 +704,21 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
if ( "HY008".equals( sqlState ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
return new QueryTimeoutException( message, sqlException, sql );
}
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( errorCode ) {
case 1222:
throw new LockTimeoutException( message, sqlException, sql );
return new LockTimeoutException( message, sqlException, sql );
case 2627:
case 2601:
throw new ConstraintViolationException( message, sqlException, sql );
return new ConstraintViolationException(
message,
sqlException,
sql,
getViolatedConstraintNameExtractor().extractConstraintName( sqlException )
);
default:
return null;
}

View File

@ -669,7 +669,7 @@ public class SybaseASEDialect extends SybaseDialect {
switch ( sqlState ) {
case "JZ0TO":
case "JZ006":
throw new LockTimeoutException( message, sqlException, sql );
return new LockTimeoutException( message, sqlException, sql );
case "S1000":
switch ( errorCode ) {
case 515:

View File

@ -52,7 +52,7 @@ public class SQLExceptionTypeDelegate extends AbstractSQLExceptionConversionDele
}
else if ( sqlException instanceof DataTruncation ||
sqlException instanceof SQLDataException ) {
throw new DataException( message, sqlException, sql );
return new DataException( message, sqlException, sql );
}
else if ( sqlException instanceof SQLIntegrityConstraintViolationException ) {
return new ConstraintViolationException(

View File

@ -117,7 +117,7 @@ public class SQLStateConversionDelegate extends AbstractSQLExceptionConversionDe
if ( "70100".equals( sqlState ) ||
// Oracle user requested cancel of current operation
( "72000".equals( sqlState ) && errorCode == 1013 ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
return new QueryTimeoutException( message, sqlException, sql );
}
}

View File

@ -11,10 +11,14 @@ import java.sql.SQLException;
import java.util.Arrays;
import java.util.BitSet;
import org.hibernate.JDBCException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.cache.spi.QueryKey;
import org.hibernate.cache.spi.QueryResultsCache;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.exception.DataException;
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.exec.ExecutionException;
@ -259,13 +263,18 @@ public class JdbcValuesResultSetImpl extends AbstractJdbcValues {
}
private ExecutionException makeExecutionException(String message, SQLException cause) {
return new ExecutionException(
message + " [" + cause.getMessage() + "]",
executionContext.getSession().getJdbcServices().getSqlExceptionHelper().convert(
cause,
message
)
final JDBCException jdbcException = executionContext.getSession().getJdbcServices().getSqlExceptionHelper().convert(
cause,
message
);
if ( jdbcException instanceof QueryTimeoutException
|| jdbcException instanceof DataException
|| jdbcException instanceof LockTimeoutException ) {
// So far, the exception helper threw these exceptions more or less directly during conversion,
// so to retain the same behavior, we throw that directly now as well instead of wrapping it
throw jdbcException;
}
return new ExecutionException( message + " [" + cause.getMessage() + "]", jdbcException );
}
private void readCurrentRowValues() {