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,10 +828,7 @@ 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 );
if ( errorCode == 131 ) {
@ -876,7 +872,6 @@ public JDBCException convert(final SQLException sqlException, final String messa
}
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,9 +552,7 @@ protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
@ -563,7 +560,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
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,9 +476,7 @@ public IdentifierHelper buildIdentifierHelper(
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
// final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
@ -487,7 +484,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
throw new LockTimeoutException( message, sqlException, sql );
}
return null;
}
};
}

View File

@ -539,9 +539,7 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
final String sqlExceptionMessage = sqlException.getMessage();
//final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
@ -594,7 +592,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
}
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,11 +264,7 @@ 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) {
return (sqlException, message, sql) -> {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch (errorCode) {
@ -286,11 +281,8 @@ public JDBCException convert(SQLException sqlException, String message, String s
}
return null;
}
};
}
return delegate;
}
@Override
public boolean supportsCurrentTimestampSelection() {

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,9 +656,7 @@ public boolean supportsLockTimeouts() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
switch ( sqlException.getErrorCode() ) {
case 1205:
case 3572:
@ -669,9 +666,7 @@ public JDBCException convert(SQLException sqlException, String message, String s
return new LockAcquisitionException( message, sqlException, sql );
}
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
switch (sqlState) {
switch ( JdbcExceptionHelper.extractSqlState( sqlException ) ) {
case "41000":
return new LockTimeoutException(message, sqlException, sql);
case "40001":
@ -679,7 +674,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
}
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,12 +725,9 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
// interpreting Oracle exceptions is much much more precise based on their specific vendor codes.
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( errorCode ) {
switch ( JdbcExceptionHelper.extractErrorCode( sqlException ) ) {
// lock timeouts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -770,7 +766,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
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,11 +554,8 @@ 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 ) {
return (sqlException, message, sql) -> {
switch ( JdbcExceptionHelper.extractSqlState( sqlException ) ) {
case "40P01":
// DEADLOCK DETECTED
return new LockAcquisitionException(message, sqlException, sql);
@ -570,7 +566,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
// 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,9 +422,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
if ( getVersion() < 9 ) {
return super.buildSQLExceptionConversionDelegate(); //null
}
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( "HY008".equals( sqlState ) ) {
@ -435,7 +432,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
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,9 +437,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return null;
}
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
return (sqlException, message, sql) -> {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
switch ( sqlState ) {
@ -456,7 +453,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
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,19 +80,13 @@ public Connection createConnection() {
return conn;
}
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess = new ValueHolder<>(
new ValueHolder.DeferredInitializer<SQLExceptionConversionDelegate>() {
@Override
public SQLExceptionConversionDelegate initialize() {
return new SQLExceptionConversionDelegate() {
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess =
new ValueHolder<>( () -> 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
@ -107,8 +99,6 @@ public JDBCException convert(SQLException sqlException, String message, String s
}
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.