Use lambda syntax to instantiate SQLExceptionConversionDelegates

Especially remove the amazingly verbose CacheSQLExceptionConversionDelegate
This commit is contained in:
gavinking 2020-02-01 14:16:44 +01:00 committed by Steve Ebersole
parent 5fc35980fd
commit d3d92f9a95
14 changed files with 239 additions and 356 deletions

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode;
@ -829,54 +828,50 @@ public String extractPattern(TemporalUnit unit) {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(final SQLException sqlException, final String message, final String sql) {
return (sqlException, message, sql) -> {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( errorCode == 131 ) {
// 131 - Transaction rolled back by lock wait timeout
return new LockTimeoutException( message, sqlException, sql );
}
if ( errorCode == 146 ) {
// 146 - Resource busy and acquire with NOWAIT specified
return new LockTimeoutException( message, sqlException, sql );
}
if ( errorCode == 132 ) {
// 132 - Transaction rolled back due to unavailable resource
return new LockAcquisitionException( message, sqlException, sql );
}
if ( errorCode == 133 ) {
// 133 - Transaction rolled back by detected deadlock
return new LockAcquisitionException( message, sqlException, sql );
}
// 259 - Invalid table name
// 260 - Invalid column name
// 261 - Invalid index name
// 262 - Invalid query name
// 263 - Invalid alias name
if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) {
throw new SQLGrammarException( message, sqlException, sql );
}
// 257 - Cannot insert NULL or update to NULL
// 301 - Unique constraint violated
// 461 - foreign key constraint violation
// 462 - failed on update or delete by foreign key constraint violation
if ( errorCode == 287 || errorCode == 301 || errorCode == 461 || errorCode == 462 ) {
final String constraintName = getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
return null;
if ( errorCode == 131 ) {
// 131 - Transaction rolled back by lock wait timeout
return new LockTimeoutException( message, sqlException, sql );
}
if ( errorCode == 146 ) {
// 146 - Resource busy and acquire with NOWAIT specified
return new LockTimeoutException( message, sqlException, sql );
}
if ( errorCode == 132 ) {
// 132 - Transaction rolled back due to unavailable resource
return new LockAcquisitionException( message, sqlException, sql );
}
if ( errorCode == 133 ) {
// 133 - Transaction rolled back by detected deadlock
return new LockAcquisitionException( message, sqlException, sql );
}
// 259 - Invalid table name
// 260 - Invalid column name
// 261 - Invalid index name
// 262 - Invalid query name
// 263 - Invalid alias name
if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) {
throw new SQLGrammarException( message, sqlException, sql );
}
// 257 - Cannot insert NULL or update to NULL
// 301 - Unique constraint violated
// 461 - foreign key constraint violation
// 462 - failed on update or delete by foreign key constraint violation
if ( errorCode == 287 || errorCode == 301 || errorCode == 461 || errorCode == 462 ) {
final String constraintName = getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
return null;
};
}

View File

@ -16,10 +16,12 @@
import org.hibernate.dialect.pagination.TopLimitHandler;
import org.hibernate.dialect.sequence.CacheSequenceSupport;
import org.hibernate.dialect.sequence.SequenceSupport;
import org.hibernate.exception.internal.CacheSQLExceptionConversionDelegate;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.DataException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.query.TemporalUnit;
import org.hibernate.query.spi.QueryEngine;
@ -318,7 +320,23 @@ public String getNoColumnsInsertString() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new CacheSQLExceptionConversionDelegate( this );
return (sqlException, message, sql) -> {
String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
if ( sqlStateClassCode != null ) {
int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( errorCode >= 119 && errorCode <= 127 && errorCode != 126 ) {
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(sqlException);
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
if ( sqlStateClassCode.equals("22")
|| sqlStateClassCode.equals("21")
|| sqlStateClassCode.equals("02") ) {
return new DataException( message, sqlException, sql );
}
}
return null; // allow other delegates the chance to look
};
}
@Override

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.NullPrecedence;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.function.CommonFunctionFactory;
@ -553,17 +552,14 @@ protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
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 );
}
return null;
if ( -952 == errorCode && "57014".equals( sqlState ) ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
};
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.boot.TempTableDdlTransactionHandling;
import org.hibernate.cfg.Environment;
@ -477,17 +476,14 @@ public IdentifierHelper buildIdentifierHelper(
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
// final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if( "40XL1".equals( sqlState ) || "40XL2".equals( sqlState ) ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
if ( "40XL1".equals( sqlState ) || "40XL2".equals( sqlState ) ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
};
}

View File

@ -539,62 +539,59 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
final String sqlExceptionMessage = sqlException.getMessage();
//final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
return (sqlException, message, sql) -> {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
final String sqlExceptionMessage = sqlException.getMessage();
//final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
// Some of the error codes will only surface in Jaybird 3 or higher, as older versions return less specific error codes first
switch ( errorCode ) {
case 335544336:
// isc_deadlock (deadlock, note: not necessarily a deadlock, can also be an update conflict)
if (sqlExceptionMessage != null
&& sqlExceptionMessage.contains( "update conflicts with concurrent update" )) {
return new LockTimeoutException( message, sqlException, sql );
}
return new LockAcquisitionException( message, sqlException, sql );
case 335544345:
// isc_lock_conflict (lock conflict on no wait transaction)
case 335544510:
// isc_lock_timeout (lock time-out on wait transaction)
// Some of the error codes will only surface in Jaybird 3 or higher, as older versions return less specific error codes first
switch ( errorCode ) {
case 335544336:
// isc_deadlock (deadlock, note: not necessarily a deadlock, can also be an update conflict)
if (sqlExceptionMessage != null
&& sqlExceptionMessage.contains( "update conflicts with concurrent update" )) {
return new LockTimeoutException( message, sqlException, sql );
case 335544474:
// isc_bad_lock_level (invalid lock level {0})
case 335544475:
// isc_relation_lock (lock on table {0} conflicts with existing lock)
case 335544476:
// isc_record_lock (requested record lock conflicts with existing lock)
return new LockAcquisitionException( message, sqlException, sql );
case 335544466:
// isc_foreign_key (violation of FOREIGN KEY constraint "{0}" on table "{1}")
case 336396758:
// *no error name* (violation of FOREIGN KEY constraint "{0}")
case 335544558:
// isc_check_constraint (Operation violates CHECK constraint {0} on view or table {1})
case 336396991:
// *no error name* (Operation violates CHECK constraint {0} on view or table)
case 335544665:
// isc_unique_key_violation (violation of PRIMARY or UNIQUE KEY constraint "{0}" on table "{1}")
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(
sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
// Apply heuristics based on exception message
String exceptionMessage = sqlException.getMessage();
if ( exceptionMessage != null ) {
if ( exceptionMessage.contains( "violation of " )
|| exceptionMessage.contains( "violates CHECK constraint" ) ) {
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(
sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
}
return null;
return new LockAcquisitionException( message, sqlException, sql );
case 335544345:
// isc_lock_conflict (lock conflict on no wait transaction)
case 335544510:
// isc_lock_timeout (lock time-out on wait transaction)
return new LockTimeoutException( message, sqlException, sql );
case 335544474:
// isc_bad_lock_level (invalid lock level {0})
case 335544475:
// isc_relation_lock (lock on table {0} conflicts with existing lock)
case 335544476:
// isc_record_lock (requested record lock conflicts with existing lock)
return new LockAcquisitionException( message, sqlException, sql );
case 335544466:
// isc_foreign_key (violation of FOREIGN KEY constraint "{0}" on table "{1}")
case 336396758:
// *no error name* (violation of FOREIGN KEY constraint "{0}")
case 335544558:
// isc_check_constraint (Operation violates CHECK constraint {0} on view or table {1})
case 336396991:
// *no error name* (Operation violates CHECK constraint {0} on view or table)
case 335544665:
// isc_unique_key_violation (violation of PRIMARY or UNIQUE KEY constraint "{0}" on table "{1}")
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(
sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
// Apply heuristics based on exception message
String exceptionMessage = sqlException.getMessage();
if ( exceptionMessage != null ) {
if ( exceptionMessage.contains( "violation of " )
|| exceptionMessage.contains( "violates CHECK constraint" ) ) {
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(
sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
}
return null;
};
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.hibernate.boot.TempTableDdlTransactionHandling;
import org.hibernate.cfg.AvailableSettings;
@ -265,31 +264,24 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
SQLExceptionConversionDelegate delegate = super.buildSQLExceptionConversionDelegate();
if (delegate == null) {
delegate = new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
return (sqlException, message, sql) -> {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch (errorCode) {
case 40001:
// DEADLOCK DETECTED
return new LockAcquisitionException(message, sqlException, sql);
case 50200:
// LOCK NOT AVAILABLE
return new PessimisticLockException(message, sqlException, sql);
case 90006:
// NULL not allowed for column [90006-145]
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(sqlException);
return new ConstraintViolationException(message, sqlException, sql, constraintName);
}
switch (errorCode) {
case 40001:
// DEADLOCK DETECTED
return new LockAcquisitionException(message, sqlException, sql);
case 50200:
// LOCK NOT AVAILABLE
return new PessimisticLockException(message, sqlException, sql);
case 90006:
// NULL not allowed for column [90006-145]
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName(sqlException);
return new ConstraintViolationException(message, sqlException, sql, constraintName);
}
return null;
}
};
}
return delegate;
return null;
};
}
@Override

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.LockOptions;
import org.hibernate.NullPrecedence;
import org.hibernate.PessimisticLockException;
@ -657,29 +656,24 @@ public boolean supportsLockTimeouts() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
switch ( sqlException.getErrorCode() ) {
case 1205:
case 3572:
return new PessimisticLockException( message, sqlException, sql );
case 1207:
case 1206:
return new LockAcquisitionException( message, sqlException, sql );
}
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
switch (sqlState) {
case "41000":
return new LockTimeoutException(message, sqlException, sql);
case "40001":
return new LockAcquisitionException(message, sqlException, sql);
}
return null;
return (sqlException, message, sql) -> {
switch ( sqlException.getErrorCode() ) {
case 1205:
case 3572:
return new PessimisticLockException( message, sqlException, sql );
case 1207:
case 1206:
return new LockAcquisitionException( message, sqlException, sql );
}
switch ( JdbcExceptionHelper.extractSqlState( sqlException ) ) {
case "41000":
return new LockTimeoutException(message, sqlException, sql);
case "40001":
return new LockAcquisitionException(message, sqlException, sql);
}
return null;
};
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.LockOptions;
import org.hibernate.QueryTimeoutException;
import org.hibernate.boot.model.TypeContributions;
@ -726,50 +725,46 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
// interpreting Oracle exceptions is much much more precise based on their specific vendor codes.
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( errorCode ) {
return (sqlException, message, sql) -> {
// interpreting Oracle exceptions is much much more precise based on their specific vendor codes.
switch ( JdbcExceptionHelper.extractErrorCode( sqlException ) ) {
// lock timeouts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// lock timeouts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 30006:
// ORA-30006: resource busy; acquire with WAIT timeout expired
throw 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);
case 4021:
// ORA-04021 timeout occurred while waiting to lock object
throw new LockTimeoutException(message, sqlException, sql);
case 30006:
// ORA-30006: resource busy; acquire with WAIT timeout expired
throw 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);
case 4021:
// ORA-04021 timeout occurred while waiting to lock object
throw new LockTimeoutException(message, sqlException, sql);
// deadlocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// deadlocks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 60:
// ORA-00060: deadlock detected while waiting for resource
return new LockAcquisitionException( message, sqlException, sql );
case 4020:
// ORA-04020 deadlock detected while trying to lock object
return new LockAcquisitionException( message, sqlException, sql );
case 60:
// ORA-00060: deadlock detected while waiting for resource
return new LockAcquisitionException( message, sqlException, sql );
case 4020:
// ORA-04020 deadlock detected while trying to lock object
return new LockAcquisitionException( message, sqlException, sql );
// query cancelled ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// query cancelled ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 1013:
// ORA-01013: user requested cancel of current operation
throw new QueryTimeoutException( message, sqlException, sql );
case 1013:
// ORA-01013: user requested cancel of current operation
throw new QueryTimeoutException( message, sqlException, sql );
// data integrity violation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// data integrity violation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
case 1407:
// ORA-01407: cannot update column to NULL
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
case 1407:
// ORA-01407: cannot update column to NULL
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
default:
return null;
}
default:
return null;
}
};
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException;
@ -555,21 +554,17 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
switch ( sqlState ) {
case "40P01":
// DEADLOCK DETECTED
return new LockAcquisitionException(message, sqlException, sql);
case "55P03":
// LOCK NOT AVAILABLE
return new PessimisticLockException(message, sqlException, sql);
default:
// returning null allows other delegates to operate
return null;
}
return (sqlException, message, sql) -> {
switch ( JdbcExceptionHelper.extractSqlState( sqlException ) ) {
case "40P01":
// DEADLOCK DETECTED
return new LockAcquisitionException(message, sqlException, sql);
case "55P03":
// LOCK NOT AVAILABLE
return new PessimisticLockException(message, sqlException, sql);
default:
// returning null allows other delegates to operate
return null;
}
};
}

View File

@ -27,7 +27,6 @@
import org.hibernate.type.descriptor.sql.SmallIntTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.sql.SQLException;
import java.sql.Types;
import java.util.regex.Pattern;
@ -423,19 +422,16 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
if ( getVersion() < 9 ) {
return super.buildSQLExceptionConversionDelegate(); //null
}
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( "HY008".equals( sqlState ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
}
if ( 1222 == errorCode ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
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 );
}
if ( 1222 == errorCode ) {
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
};
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect;
import org.hibernate.JDBCException;
import org.hibernate.LockOptions;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.TopLimitHandler;
@ -438,25 +437,22 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return null;
}
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( sqlState ) {
case "JZ0TO":
case "JZ006":
throw new LockTimeoutException( message, sqlException, sql );
case "ZZZZZ":
if (515 == errorCode) {
// Attempt to insert NULL value into column; column does not allow nulls.
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
break;
}
return null;
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( sqlState ) {
case "JZ0TO":
case "JZ006":
throw new LockTimeoutException( message, sqlException, sql );
case "ZZZZZ":
if (515 == errorCode) {
// Attempt to insert NULL value into column; column does not allow nulls.
final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
break;
}
return null;
};
}

View File

@ -15,9 +15,7 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.spi.ConversionContext;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.util.ValueHolder;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
@ -82,32 +80,24 @@ public Connection createConnection() {
return conn;
}
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess = new ValueHolder<>(
new ValueHolder.DeferredInitializer<SQLExceptionConversionDelegate>() {
@Override
public SQLExceptionConversionDelegate initialize() {
return new SQLExceptionConversionDelegate() {
private final SQLStateConversionDelegate sqlStateDelegate = new SQLStateConversionDelegate(
new ConversionContext() {
@Override
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
// this should never happen...
throw new HibernateException( "Unexpected call to org.hibernate.exception.spi.ConversionContext.getViolatedConstraintNameExtracter" );
}
}
);
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
JDBCException exception = sqlStateDelegate.convert( sqlException, message, sql );
if ( exception == null ) {
// assume this is either a set-up problem or a problem connecting, which we will
// categorize the same here.
exception = new JDBCConnectionException( message, sqlException, sql );
}
return exception;
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess =
new ValueHolder<>( () -> new SQLExceptionConversionDelegate() {
private final SQLStateConversionDelegate sqlStateDelegate = new SQLStateConversionDelegate(
() -> {
// this should never happen...
throw new HibernateException( "Unexpected call to org.hibernate.exception.spi.ConversionContext.getViolatedConstraintNameExtracter" );
}
};
);
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
JDBCException exception = sqlStateDelegate.convert( sqlException, message, sql );
if ( exception == null ) {
// assume this is either a set-up problem or a problem connecting, which we will
// categorize the same here.
exception = new JDBCConnectionException( message, sqlException, sql );
}
return exception;
}
}
);
@ -133,7 +123,6 @@ protected JDBCException convertSqlException(String message, SQLException e) {
/**
* Exposed for testing purposes only.
* @return
*/
public Properties getConnectionProperties() {
return new Properties( connectionProps );

View File

@ -1,77 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.exception.internal;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.JDBCException;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.DataException;
import org.hibernate.exception.spi.AbstractSQLExceptionConversionDelegate;
import org.hibernate.exception.spi.ConversionContext;
import org.hibernate.internal.util.JdbcExceptionHelper;
/**
* A {@link org.hibernate.exception.spi.SQLExceptionConversionDelegate}
* implementation specific to Cach&eacute; SQL, accounting for its custom
* integrity constraint violation error codes.
*
* @author Jonathan Levinson
*/
public class CacheSQLExceptionConversionDelegate extends AbstractSQLExceptionConversionDelegate {
private static final Set<String> DATA_CATEGORIES = new HashSet<>();
private static final Set<Integer> INTEGRITY_VIOLATION_CATEGORIES = new HashSet<>();
static {
DATA_CATEGORIES.add( "22" );
DATA_CATEGORIES.add( "21" );
DATA_CATEGORIES.add( "02" );
INTEGRITY_VIOLATION_CATEGORIES.add( 119 );
INTEGRITY_VIOLATION_CATEGORIES.add( 120 );
INTEGRITY_VIOLATION_CATEGORIES.add( 121 );
INTEGRITY_VIOLATION_CATEGORIES.add( 122 );
INTEGRITY_VIOLATION_CATEGORIES.add( 123 );
INTEGRITY_VIOLATION_CATEGORIES.add( 124 );
INTEGRITY_VIOLATION_CATEGORIES.add( 125 );
INTEGRITY_VIOLATION_CATEGORIES.add( 127 );
}
public CacheSQLExceptionConversionDelegate(ConversionContext conversionContext) {
super( conversionContext );
}
/**
* Convert the given SQLException into Hibernate's JDBCException hierarchy.
*
* @param sqlException The SQLException to be converted.
* @param message An optional error message.
* @param sql Optionally, the sql being performed when the exception occurred.
* @return The resulting JDBCException; returns null if it could not be converted.
*/
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
if ( sqlStateClassCode != null ) {
Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
String constraintName =
getConversionContext()
.getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
return new DataException( message, sqlException, sql );
}
}
return null; // allow other delegates the chance to look
}
}

View File

@ -16,6 +16,7 @@
*
* @author Steve Ebersole
*/
@FunctionalInterface
public interface SQLExceptionConversionDelegate {
/**
* Convert the given SQLException into the Hibernate {@link org.hibernate.JDBCException} hierarchy.