From 6b51952137b95ba97c4d93b2e625a64f0b2273d9 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Tue, 25 Jan 2022 21:26:34 +0100 Subject: [PATCH] clean up SQLExceptionConverter and friends removing obsolete stuff --- .../spi/BasicSQLExceptionConverter.java | 9 ++++-- .../engine/jdbc/spi/SqlExceptionHelper.java | 10 +++--- .../internal/SQLExceptionTypeDelegate.java | 25 ++++++++------- .../internal/SQLStateConversionDelegate.java | 18 +++++------ .../exception/internal/SQLStateConverter.java | 31 ------------------- .../StandardSQLExceptionConverter.java | 30 +++++++++++++++--- .../hibernate/exception/spi/Configurable.java | 28 ----------------- .../spi/SQLExceptionConversionDelegate.java | 18 ++++++----- .../exception/spi/SQLExceptionConverter.java | 21 ++++++------- ...platedViolatedConstraintNameExtractor.java | 9 +++--- .../spi/ViolatedConstraintNameExtractor.java | 8 ++--- 11 files changed, 88 insertions(+), 119 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConverter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/exception/spi/Configurable.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicSQLExceptionConverter.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicSQLExceptionConverter.java index 6ca78186ee..af0ccd1bcd 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicSQLExceptionConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicSQLExceptionConverter.java @@ -9,8 +9,9 @@ package org.hibernate.engine.jdbc.dialect.spi; import java.sql.SQLException; import org.hibernate.JDBCException; -import org.hibernate.exception.internal.SQLStateConverter; -import org.hibernate.exception.spi.ViolatedConstraintNameExtractor; +import org.hibernate.exception.internal.SQLStateConversionDelegate; +import org.hibernate.exception.internal.StandardSQLExceptionConverter; +import org.hibernate.exception.spi.SQLExceptionConverter; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; @@ -34,7 +35,9 @@ public class BasicSQLExceptionConverter { */ public static final String MSG = LOG.unableToQueryDatabaseMetadata(); - private static final SQLStateConverter CONVERTER = new SQLStateConverter( sqle ->"???" ); + private static final SQLExceptionConverter CONVERTER = new StandardSQLExceptionConverter( + new SQLStateConversionDelegate(() -> sqle ->"???" ) + ); /** * Perform a conversion. diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/SqlExceptionHelper.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/SqlExceptionHelper.java index 3061699505..4ea09d9910 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/SqlExceptionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/SqlExceptionHelper.java @@ -14,9 +14,9 @@ import java.util.ArrayList; import java.util.List; import org.hibernate.JDBCException; -import org.hibernate.exception.internal.SQLStateConverter; +import org.hibernate.exception.internal.SQLStateConversionDelegate; +import org.hibernate.exception.internal.StandardSQLExceptionConverter; import org.hibernate.exception.spi.SQLExceptionConverter; -import org.hibernate.exception.spi.ViolatedConstraintNameExtractor; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.StringHelper; @@ -38,7 +38,9 @@ public class SqlExceptionHelper { private static final String DEFAULT_WARNING_MSG = "SQL Warning"; private final boolean logWarnings; - private static final SQLExceptionConverter DEFAULT_CONVERTER = new SQLStateConverter( e -> null ); + private static final SQLExceptionConverter DEFAULT_CONVERTER = new StandardSQLExceptionConverter( + new SQLStateConversionDelegate( () -> e -> null ) + ); private SQLExceptionConverter sqlExceptionConverter; @@ -76,7 +78,7 @@ public class SqlExceptionHelper { * @param sqlExceptionConverter The converter to use. */ public void setSqlExceptionConverter(SQLExceptionConverter sqlExceptionConverter) { - this.sqlExceptionConverter = ( sqlExceptionConverter == null ? DEFAULT_CONVERTER : sqlExceptionConverter ); + this.sqlExceptionConverter = sqlExceptionConverter == null ? DEFAULT_CONVERTER : sqlExceptionConverter; } // SQLException ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLExceptionTypeDelegate.java b/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLExceptionTypeDelegate.java index 7d0ef4acdd..803d9fbbfd 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLExceptionTypeDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLExceptionTypeDelegate.java @@ -29,8 +29,9 @@ import org.hibernate.exception.spi.AbstractSQLExceptionConversionDelegate; import org.hibernate.exception.spi.ConversionContext; /** - * {@link org.hibernate.exception.spi.SQLExceptionConverter} implementation that does conversion based on the - * JDBC 4 defined {@link SQLException} sub-type hierarchy. + * A {@link org.hibernate.exception.spi.SQLExceptionConverter} implementation + * that does conversion based on the {@link SQLException} subtype hierarchy + * defined by JDBC 4. * * @author Steve Ebersole */ @@ -41,17 +42,17 @@ public class SQLExceptionTypeDelegate extends AbstractSQLExceptionConversionDele @Override public JDBCException convert(SQLException sqlException, String message, String sql) { - if ( SQLClientInfoException.class.isInstance( sqlException ) - || SQLInvalidAuthorizationSpecException.class.isInstance( sqlException ) - || SQLNonTransientConnectionException.class.isInstance( sqlException ) - || SQLTransientConnectionException.class.isInstance( sqlException ) ) { + if ( sqlException instanceof SQLClientInfoException + || sqlException instanceof SQLInvalidAuthorizationSpecException + || sqlException instanceof SQLNonTransientConnectionException + || sqlException instanceof SQLTransientConnectionException ) { return new JDBCConnectionException( message, sqlException, sql ); } - else if ( DataTruncation.class.isInstance( sqlException ) || - SQLDataException.class.isInstance( sqlException ) ) { + else if ( sqlException instanceof DataTruncation || + sqlException instanceof SQLDataException ) { throw new DataException( message, sqlException, sql ); } - else if ( SQLIntegrityConstraintViolationException.class.isInstance( sqlException ) ) { + else if ( sqlException instanceof SQLIntegrityConstraintViolationException ) { return new ConstraintViolationException( message, sqlException, @@ -59,13 +60,13 @@ public class SQLExceptionTypeDelegate extends AbstractSQLExceptionConversionDele getConversionContext().getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) ); } - else if ( SQLSyntaxErrorException.class.isInstance( sqlException ) ) { + else if ( sqlException instanceof SQLSyntaxErrorException ) { return new SQLGrammarException( message, sqlException, sql ); } - else if ( SQLTimeoutException.class.isInstance( sqlException ) ) { + else if ( sqlException instanceof SQLTimeoutException ) { return new QueryTimeoutException( message, sqlException, sql ); } - else if ( SQLTransactionRollbackException.class.isInstance( sqlException ) ) { + else if ( sqlException instanceof SQLTransactionRollbackException ) { // Not 100% sure this is completely accurate. The JavaDocs for SQLTransactionRollbackException state that // it indicates sql states starting with '40' and that those usually indicate that: // diff --git a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConversionDelegate.java b/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConversionDelegate.java index aebf768a73..3e869900ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConversionDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConversionDelegate.java @@ -22,14 +22,14 @@ import org.hibernate.exception.spi.ConversionContext; import org.hibernate.internal.util.JdbcExceptionHelper; /** - * A SQLExceptionConverter implementation which performs conversion based on the underlying SQLState. - * Interpretation of a SQL error based on SQLState is not nearly as accurate as using the ErrorCode (which is, - * however, vendor-specific). - * - * SQLState codes are defined by both ANSI SQL specs and X/Open. Some of the "classes" are shared, others are + * A {@link org.hibernate.exception.spi.SQLExceptionConverter} implementation which performs conversion based + * on the underlying SQLState. Interpretation of a SQL error based on SQLState is not nearly as accurate as + * using the ErrorCode (which is, however, vendor-specific). + *

+ * SQLState codes are defined by both ANSI SQL specs and X/Open. Some "classes" are shared, others are * specific to one or another, yet others are custom vendor classes. Unfortunately I have not been able to * find a "blessed" list of X/Open codes. These codes are cobbled together between ANSI SQL spec and error - * code tables from few vendors documentation. + * code tables from few vendor's documentation. * * @author Steve Ebersole */ @@ -48,7 +48,7 @@ public class SQLStateConversionDelegate extends AbstractSQLExceptionConversionDe ); } - private static final Set DATA_CATEGORIES = buildDataCategories(); + private static final Set DATA_CATEGORIES = buildDataCategories(); private static Set buildDataCategories() { return Set.of( "21", // "cardinality violation" @@ -56,7 +56,7 @@ public class SQLStateConversionDelegate extends AbstractSQLExceptionConversionDe ); } - private static final Set INTEGRITY_VIOLATION_CATEGORIES = buildContraintCategories(); + private static final Set INTEGRITY_VIOLATION_CATEGORIES = buildContraintCategories(); private static Set buildContraintCategories() { return Set.of( "23", // "integrity constraint violation" @@ -65,7 +65,7 @@ public class SQLStateConversionDelegate extends AbstractSQLExceptionConversionDe ); } - private static final Set CONNECTION_CATEGORIES = buildConnectionCategories(); + private static final Set CONNECTION_CATEGORIES = buildConnectionCategories(); private static Set buildConnectionCategories() { return Set.of( "08" // "connection exception" diff --git a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConverter.java b/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConverter.java deleted file mode 100644 index 74d8080101..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/exception/internal/SQLStateConverter.java +++ /dev/null @@ -1,31 +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 . - */ -package org.hibernate.exception.internal; - -import org.hibernate.exception.spi.ConversionContext; -import org.hibernate.exception.spi.SQLExceptionConverter; -import org.hibernate.exception.spi.ViolatedConstraintNameExtractor; - -/** - * @deprecated Use {@link StandardSQLExceptionConverter} with {@link SQLStateConversionDelegate} - * instead - * - * @author Steve Ebersole - */ -@Deprecated -public class SQLStateConverter extends StandardSQLExceptionConverter implements SQLExceptionConverter { - public SQLStateConverter(final ViolatedConstraintNameExtractor extractor) { - super(); - final ConversionContext conversionContext = new ConversionContext() { - @Override - public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() { - return extractor; - } - }; - addDelegate( new SQLStateConversionDelegate( conversionContext ) ); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java b/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java index 68df393eca..055b8e42c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/internal/StandardSQLExceptionConverter.java @@ -6,24 +6,44 @@ */ package org.hibernate.exception.internal; -import java.sql.SQLException; -import java.util.ArrayList; - import org.hibernate.JDBCException; import org.hibernate.exception.GenericJDBCException; import org.hibernate.exception.spi.SQLExceptionConversionDelegate; import org.hibernate.exception.spi.SQLExceptionConverter; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** + * A {@link SQLExceptionConverter} that delegates to a chain of + * {@link SQLExceptionConversionDelegate}. + * * @author Steve Ebersole */ public class StandardSQLExceptionConverter implements SQLExceptionConverter { - private final ArrayList delegates = new ArrayList<>(); + private final List delegates; - public StandardSQLExceptionConverter() { + public StandardSQLExceptionConverter(SQLExceptionConversionDelegate... delegates) { + this.delegates = Arrays.asList(delegates); } + /** + * @deprecated use {@link #StandardSQLExceptionConverter(SQLExceptionConversionDelegate...)} + */ + @Deprecated(since = "6.0") + public StandardSQLExceptionConverter() { + delegates = new ArrayList<>(); + } + + /** + * Add a delegate. + * + * @deprecated use {@link #StandardSQLExceptionConverter(SQLExceptionConversionDelegate...)} + */ + @Deprecated(since = "6.0") public void addDelegate(SQLExceptionConversionDelegate delegate) { if ( delegate != null ) { this.delegates.add( delegate ); diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/Configurable.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/Configurable.java deleted file mode 100644 index bbbe8082ed..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/Configurable.java +++ /dev/null @@ -1,28 +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 . - */ -package org.hibernate.exception.spi; - -import java.util.Properties; - -import org.hibernate.HibernateException; - -/** - * This interface defines the contract for {@link SQLExceptionConverter} - * implementations that want to be configured prior to usage given the - * currently defined Hibernate properties. - * - * @author Steve Ebersole - */ -public interface Configurable { - /** - * Configure the component, using the given settings and properties. - * - * @param properties All defined startup properties. - * @throws HibernateException Indicates a configuration exception. - */ - void configure(Properties properties) throws HibernateException; -} diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConversionDelegate.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConversionDelegate.java index a763caf64f..5d20e58196 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConversionDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConversionDelegate.java @@ -11,21 +11,25 @@ import java.sql.SQLException; import org.hibernate.JDBCException; /** - * Allow a {@link SQLExceptionConverter} to work by chaining together multiple such delegates. The main - * difference between a delegate and a full-fledged converter is that a delegate may return {@code null}. + * Allow a {@link SQLExceptionConverter} to work by chaining together + * multiple delegates. The main difference between a delegate and a + * full-fledged converter is that a delegate may return {@code null}. * * @author Steve Ebersole */ @FunctionalInterface public interface SQLExceptionConversionDelegate { /** - * Convert the given SQLException into the Hibernate {@link JDBCException} hierarchy. + * Convert the given {@link SQLException} to a subtype of + * {@link JDBCException}, if possible. * - * @param sqlException The SQLException to be converted. - * @param message An (optional) error message. - * @param sql The {@literal SQL} statement, if one, being performed when the exception occurred. + * @param sqlException The {@code SQLException} to be converted + * @param message An optional error message + * @param sql The SQL statement that resulted in the exception * - * @return The resulting JDBCException, can be {@code null} + * @return The resulting {@code JDBCException}, or {@code null} + * if this delegate does not know how to interpret the + * given {@link SQLException}. */ JDBCException convert(SQLException sqlException, String message, String sql); diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConverter.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConverter.java index 1e8650617e..8a08f7face 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/spi/SQLExceptionConverter.java @@ -12,24 +12,21 @@ import java.sql.SQLException; import org.hibernate.JDBCException; /** - * Defines a contract for implementations that know how to convert a - * {@link SQLException} to Hibernate's {@link JDBCException} hierarchy. - *

- * Implementations must have a constructor which takes a - * {@link ViolatedConstraintNameExtractor} parameter. - *

- * Implementations may implement {@link Configurable} if they need to - * perform configuration steps prior to first use. + * An object that interprets JDBC {@link SQLException}s and converts + * them to subtypes of Hibernate {@link JDBCException}s. * * @author Steve Ebersole */ public interface SQLExceptionConverter extends Serializable { /** - * Convert the given SQLException into the Hibernate {@link JDBCException} hierarchy. + * Convert the given {@link SQLException} to a subtype of + * {@link JDBCException}. * - * @param sqlException The SQLException to be converted. - * @param message An optional error message. - * @return The resulting JDBCException. + * @param sqlException The {@code SQLException} to be converted + * @param message An optional error message + * @param sql The SQL statement that resulted in the exception + * + * @return The resulting {@code JDBCException}. * * @see org.hibernate.exception.ConstraintViolationException * @see org.hibernate.exception.JDBCConnectionException diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java index fb053e4435..c032cef912 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/spi/TemplatedViolatedConstraintNameExtractor.java @@ -10,15 +10,15 @@ import java.sql.SQLException; import java.util.function.Function; /** - * Knows how to extract a violated constraint name from an error message based on the - * fact that the constraint name is templated within the message. + * Extracts a violated database constraint name from an error message + * by matching the error message against a template. * * @author Steve Ebersole * @author Brett Meyer */ public class TemplatedViolatedConstraintNameExtractor implements ViolatedConstraintNameExtractor { - private Function extractConstraintName; + private final Function extractConstraintName; public TemplatedViolatedConstraintNameExtractor(Function extractConstraintName) { this.extractConstraintName = extractConstraintName; @@ -49,7 +49,8 @@ public class TemplatedViolatedConstraintNameExtractor implements ViolatedConstra } /** - * Extracts the constraint name based on a template (i.e., templateStartconstraintNametemplateEnd). + * Extracts the constraint name based on a template of form + * templateStartconstraintNametemplateEnd. * * @param templateStart The pattern denoting the start of the constraint name within the message. * @param templateEnd The pattern denoting the end of the constraint name within the message. diff --git a/hibernate-core/src/main/java/org/hibernate/exception/spi/ViolatedConstraintNameExtractor.java b/hibernate-core/src/main/java/org/hibernate/exception/spi/ViolatedConstraintNameExtractor.java index 644f7cc6d4..3e1abd222d 100644 --- a/hibernate-core/src/main/java/org/hibernate/exception/spi/ViolatedConstraintNameExtractor.java +++ b/hibernate-core/src/main/java/org/hibernate/exception/spi/ViolatedConstraintNameExtractor.java @@ -9,16 +9,16 @@ package org.hibernate.exception.spi; import java.sql.SQLException; /** - * Defines a contract for implementations that can extract the name of a violated - * constraint from a {@link SQLException} that is the result of that constraint - * violation. + * An object that can extract the name of a violated database constraint + * from a {@link SQLException} that results from the constraint violation. * * @author Steve Ebersole */ @FunctionalInterface public interface ViolatedConstraintNameExtractor { /** - * Extract the name of the violated constraint from the given SQLException. + * Extract the name of the violated constraint from the given + * {@link SQLException}. * * @param sqle The exception that was the result of the constraint violation. * @return The extracted constraint name.