From 1a1523db44dcf8444888ba4dc81db546f964c800 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 28 Jan 2016 18:24:55 +0000 Subject: [PATCH] HHH-10484 - Add hibernate.jdbc.log.warnings configuration property --- .../org/hibernate/cfg/AvailableSettings.java | 10 ++++++++++ .../engine/jdbc/ResultSetWrapperProxy.java | 2 +- .../env/internal/JdbcEnvironmentImpl.java | 19 ++++++++++++++----- .../engine/jdbc/spi/SqlExceptionHelper.java | 14 +++++++++----- .../java/org/hibernate/jdbc/Expectations.java | 2 +- .../ManagedProviderConnectionHelper.java | 9 ++++++++- .../hbm2ddl/SuppliedConnectionHelper.java | 6 ++++-- ...iedConnectionProviderConnectionHelper.java | 6 ++++-- .../HibernateSchemaManagementTool.java | 5 ++++- .../schema/internal/SchemaDropperImpl.java | 2 ++ .../exec/GenerationTargetToDatabase.java | 7 ++++++- 11 files changed, 63 insertions(+), 19 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 41cceab53d..995064ac5d 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -590,6 +590,16 @@ public interface AvailableSettings { */ String DEFAULT_NULL_ORDERING = "hibernate.order_by.default_null_ordering"; + /** + * Enable fetching JDBC statement warning for logging. + * + * Values are {@code true} or {@code false} . + * Default value is {@code false} + * + * @since 5.1 + */ + String LOG_JDBC_WARNINGS = "hibernate.jdbc.log.warnings"; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java index fc7e3dfed4..f9027faeb5 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java @@ -29,7 +29,7 @@ import static org.hibernate.internal.CoreLogging.messageLogger; public class ResultSetWrapperProxy implements InvocationHandler { private static final CoreMessageLogger LOG = messageLogger( ResultSetWrapperProxy.class ); - private static final SqlExceptionHelper SQL_EXCEPTION_HELPER = new SqlExceptionHelper(); + private static final SqlExceptionHelper SQL_EXCEPTION_HELPER = new SqlExceptionHelper( false ); private final ResultSet rs; private final ColumnNameCache columnNameCache; diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java index ec509aa000..c9026412cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentImpl.java @@ -32,6 +32,7 @@ import org.hibernate.engine.jdbc.spi.TypeInfo; import org.hibernate.exception.internal.SQLExceptionTypeDelegate; import org.hibernate.exception.internal.SQLStateConversionDelegate; import org.hibernate.exception.internal.StandardSQLExceptionConverter; +import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.jboss.logging.Logger; @@ -73,7 +74,7 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { } this.nameQualifierSupport = nameQualifierSupport; - this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); + this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService ) ); this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ).build(); final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this ); @@ -107,6 +108,14 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder(); } + private static boolean logWarnings(ConfigurationService cfgService) { + return cfgService.getSetting( + AvailableSettings.LOG_JDBC_WARNINGS, + StandardConverters.BOOLEAN, + false + ); + } + private static boolean globalQuoting(ConfigurationService cfgService) { return cfgService.getSetting( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, @@ -140,7 +149,7 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException { this.dialect = dialect; - this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); + this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false ); this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ) .apply( databaseMetaData ) @@ -213,7 +222,7 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); - this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); + this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService ) ); this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ) .apply( databaseMetaData ) @@ -291,13 +300,13 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment { } @SuppressWarnings("deprecation") - private SqlExceptionHelper buildSqlExceptionHelper(Dialect dialect) { + private SqlExceptionHelper buildSqlExceptionHelper(Dialect dialect, boolean logWarnings) { final StandardSQLExceptionConverter sqlExceptionConverter = new StandardSQLExceptionConverter(); sqlExceptionConverter.addDelegate( dialect.buildSQLExceptionConversionDelegate() ); sqlExceptionConverter.addDelegate( new SQLExceptionTypeDelegate( dialect ) ); // todo : vary this based on extractedMetaDataSupport.getSqlStateType() sqlExceptionConverter.addDelegate( new SQLStateConversionDelegate( dialect ) ); - return new SqlExceptionHelper( sqlExceptionConverter ); + return new SqlExceptionHelper( sqlExceptionConverter, logWarnings ); } private Set buildMergedReservedWords(Dialect dialect, DatabaseMetaData dbmd) throws SQLException { 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 426f8c175a..94cea43100 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 @@ -34,6 +34,7 @@ public class SqlExceptionHelper { private static final String DEFAULT_EXCEPTION_MSG = "SQL Exception"; private static final String DEFAULT_WARNING_MSG = "SQL Warning"; + private final boolean logWarnings; private static final SQLExceptionConverter DEFAULT_CONVERTER = new SQLStateConverter( new ViolatedConstraintNameExtracter() { @@ -48,8 +49,8 @@ public class SqlExceptionHelper { /** * Create an exception helper with a default exception converter. */ - public SqlExceptionHelper() { - sqlExceptionConverter = DEFAULT_CONVERTER; + public SqlExceptionHelper( boolean logWarnings) { + this( DEFAULT_CONVERTER, logWarnings ); } /** @@ -57,8 +58,9 @@ public class SqlExceptionHelper { * * @param sqlExceptionConverter The exception converter to use. */ - public SqlExceptionHelper(SQLExceptionConverter sqlExceptionConverter) { + public SqlExceptionHelper(SQLExceptionConverter sqlExceptionConverter, boolean logWarnings) { this.sqlExceptionConverter = sqlExceptionConverter; + this.logWarnings = logWarnings; } /** @@ -271,7 +273,9 @@ public class SqlExceptionHelper { Connection connection, WarningHandler handler) { try { - walkWarnings( connection.getWarnings(), handler ); + if ( logWarnings ) { + walkWarnings( connection.getWarnings(), handler ); + } } catch (SQLException sqle) { // workaround for WebLogic @@ -300,7 +304,7 @@ public class SqlExceptionHelper { WarningHandler handler) { // See HHH-9174. Statement#getWarnings can be an expensive call for many JDBC libs. Don't do it unless // the log level would actually allow a warning to be logged. - if ( LOG.isEnabled( Level.WARN ) ) { + if ( logWarnings ) { try { walkWarnings( statement.getWarnings(), handler ); } diff --git a/hibernate-core/src/main/java/org/hibernate/jdbc/Expectations.java b/hibernate-core/src/main/java/org/hibernate/jdbc/Expectations.java index 76f7b58c2c..e540b4bbc9 100644 --- a/hibernate-core/src/main/java/org/hibernate/jdbc/Expectations.java +++ b/hibernate-core/src/main/java/org/hibernate/jdbc/Expectations.java @@ -27,7 +27,7 @@ import org.hibernate.internal.CoreMessageLogger; public class Expectations { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( Expectations.class ); - private static SqlExceptionHelper sqlExceptionHelper = new SqlExceptionHelper(); + private static SqlExceptionHelper sqlExceptionHelper = new SqlExceptionHelper( false ); public static final int USUAL_EXPECTED_COUNT = 1; public static final int USUAL_PARAM_POSITION = 1; diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java index b50b82cf87..40f980c415 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/ManagedProviderConnectionHelper.java @@ -12,7 +12,9 @@ import java.util.Properties; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; +import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.internal.util.config.ConfigurationHelper; @@ -67,7 +69,12 @@ class ManagedProviderConnectionHelper implements ConnectionHelper { private void releaseConnection() throws SQLException { if ( connection != null ) { try { - new SqlExceptionHelper().logAndClearWarnings( connection ); + final boolean logWarning = ConfigurationHelper.getBoolean( + AvailableSettings.LOG_JDBC_WARNINGS, + cfgProperties, + false + ); + new SqlExceptionHelper( logWarning ).logAndClearWarnings( connection ); } finally { try { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionHelper.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionHelper.java index 05040a1cc2..0a9f9d38b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionHelper.java @@ -24,9 +24,11 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; class SuppliedConnectionHelper implements ConnectionHelper { private Connection connection; private boolean toggleAutoCommit; + private final SqlExceptionHelper sqlExceptionHelper; - public SuppliedConnectionHelper(Connection connection) { + public SuppliedConnectionHelper(Connection connection, SqlExceptionHelper sqlExceptionHelper) { this.connection = connection; + this.sqlExceptionHelper = sqlExceptionHelper; } public void prepare(boolean needsAutoCommit) throws SQLException { @@ -47,7 +49,7 @@ class SuppliedConnectionHelper implements ConnectionHelper { } public void release() throws SQLException { - new SqlExceptionHelper().logAndClearWarnings( connection ); + sqlExceptionHelper.logAndClearWarnings( connection ); if ( toggleAutoCommit ) { connection.setAutoCommit( false ); } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionProviderConnectionHelper.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionProviderConnectionHelper.java index 8be6be8cdc..b0d4e121dd 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionProviderConnectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SuppliedConnectionProviderConnectionHelper.java @@ -28,9 +28,11 @@ class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper { private ConnectionProvider provider; private Connection connection; private boolean toggleAutoCommit; + private final SqlExceptionHelper sqlExceptionHelper; - public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) { + public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider, SqlExceptionHelper sqlExceptionHelper) { this.provider = provider; + this.sqlExceptionHelper = sqlExceptionHelper; } public void prepare(boolean needsAutoCommit) throws SQLException { @@ -54,7 +56,7 @@ class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper { public void release() throws SQLException { // we only release the connection if ( connection != null ) { - new SqlExceptionHelper().logAndClearWarnings( connection ); + sqlExceptionHelper.logAndClearWarnings( connection ); if ( toggleAutoCommit ) { connection.setAutoCommit( false ); } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java index f95b901b2a..c0d23a6f94 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/HibernateSchemaManagementTool.java @@ -15,6 +15,7 @@ import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; import org.hibernate.engine.jdbc.dialect.spi.DialectResolver; +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.internal.util.StringHelper; @@ -122,6 +123,7 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv jdbcContext.getSqlStatementLogger(), needsAutoCommit ) + , serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper() ); } @@ -153,7 +155,8 @@ public class HibernateSchemaManagementTool implements SchemaManagementTool, Serv } if ( targetDescriptor.getTargetTypes().contains( TargetType.DATABASE ) ) { - targets[index] = new GenerationTargetToDatabase( connectionContext ); + targets[index] = new GenerationTargetToDatabase( connectionContext + , serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper() ); } return targets; diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java index a0bf4e800e..9059799383 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java @@ -453,6 +453,7 @@ public class SchemaDropperImpl implements SchemaDropper { jdbcContext.getSqlStatementLogger(), true ) + , serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper() ) }; } @@ -535,6 +536,7 @@ public class SchemaDropperImpl implements SchemaDropper { serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(), true ) + , serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper() ); target.prepare(); try { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToDatabase.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToDatabase.java index 43cf227a90..9c3bfbef7d 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToDatabase.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToDatabase.java @@ -24,13 +24,18 @@ import org.hibernate.tool.schema.spi.SchemaManagementException; public class GenerationTargetToDatabase implements GenerationTarget { private static final CoreMessageLogger log = CoreLogging.messageLogger( GenerationTargetToDatabase.class ); - private final SqlExceptionHelper sqlExceptionHelper = new SqlExceptionHelper(); + private final SqlExceptionHelper sqlExceptionHelper; private final JdbcConnectionContext jdbcConnectionContext; private Statement jdbcStatement; public GenerationTargetToDatabase(JdbcConnectionContext jdbcConnectionContext) { + this( jdbcConnectionContext, new SqlExceptionHelper( true ) ); + } + + public GenerationTargetToDatabase(JdbcConnectionContext jdbcConnectionContext, SqlExceptionHelper sqlExceptionHelper) { this.jdbcConnectionContext = jdbcConnectionContext; + this.sqlExceptionHelper = sqlExceptionHelper; } @Override