HHH-10484 - Add hibernate.jdbc.log.warnings configuration property

This commit is contained in:
Andrea Boriero 2016-01-28 18:24:55 +00:00
parent e01e9b52fb
commit 1a1523db44
11 changed files with 63 additions and 19 deletions

View File

@ -590,6 +590,16 @@ public interface AvailableSettings {
*/ */
String DEFAULT_NULL_ORDERING = "hibernate.order_by.default_null_ordering"; 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";
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -29,7 +29,7 @@
public class ResultSetWrapperProxy implements InvocationHandler { public class ResultSetWrapperProxy implements InvocationHandler {
private static final CoreMessageLogger LOG = messageLogger( ResultSetWrapperProxy.class ); 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 ResultSet rs;
private final ColumnNameCache columnNameCache; private final ColumnNameCache columnNameCache;

View File

@ -32,6 +32,7 @@
import org.hibernate.exception.internal.SQLExceptionTypeDelegate; import org.hibernate.exception.internal.SQLExceptionTypeDelegate;
import org.hibernate.exception.internal.SQLStateConversionDelegate; import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.internal.StandardSQLExceptionConverter; import org.hibernate.exception.internal.StandardSQLExceptionConverter;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -73,7 +74,7 @@ public JdbcEnvironmentImpl(ServiceRegistryImplementor serviceRegistry, Dialect d
} }
this.nameQualifierSupport = nameQualifierSupport; this.nameQualifierSupport = nameQualifierSupport;
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService ) );
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ).build(); this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ).build();
final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this ); final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
@ -107,6 +108,14 @@ public JdbcEnvironmentImpl(ServiceRegistryImplementor serviceRegistry, Dialect d
this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder(); 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) { private static boolean globalQuoting(ConfigurationService cfgService) {
return cfgService.getSetting( return cfgService.getSetting(
AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS,
@ -140,7 +149,7 @@ private static boolean autoKeywordQuoting(ConfigurationService cfgService) {
public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException { public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException {
this.dialect = dialect; this.dialect = dialect;
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false );
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ) this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this )
.apply( databaseMetaData ) .apply( databaseMetaData )
@ -213,7 +222,7 @@ public JdbcEnvironmentImpl(
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect ); this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService ) );
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this ) this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this )
.apply( databaseMetaData ) .apply( databaseMetaData )
@ -291,13 +300,13 @@ private String determineCurrentSchemaName(
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private SqlExceptionHelper buildSqlExceptionHelper(Dialect dialect) { private SqlExceptionHelper buildSqlExceptionHelper(Dialect dialect, boolean logWarnings) {
final StandardSQLExceptionConverter sqlExceptionConverter = new StandardSQLExceptionConverter(); final StandardSQLExceptionConverter sqlExceptionConverter = new StandardSQLExceptionConverter();
sqlExceptionConverter.addDelegate( dialect.buildSQLExceptionConversionDelegate() ); sqlExceptionConverter.addDelegate( dialect.buildSQLExceptionConversionDelegate() );
sqlExceptionConverter.addDelegate( new SQLExceptionTypeDelegate( dialect ) ); sqlExceptionConverter.addDelegate( new SQLExceptionTypeDelegate( dialect ) );
// todo : vary this based on extractedMetaDataSupport.getSqlStateType() // todo : vary this based on extractedMetaDataSupport.getSqlStateType()
sqlExceptionConverter.addDelegate( new SQLStateConversionDelegate( dialect ) ); sqlExceptionConverter.addDelegate( new SQLStateConversionDelegate( dialect ) );
return new SqlExceptionHelper( sqlExceptionConverter ); return new SqlExceptionHelper( sqlExceptionConverter, logWarnings );
} }
private Set<String> buildMergedReservedWords(Dialect dialect, DatabaseMetaData dbmd) throws SQLException { private Set<String> buildMergedReservedWords(Dialect dialect, DatabaseMetaData dbmd) throws SQLException {

View File

@ -34,6 +34,7 @@ public class SqlExceptionHelper {
private static final String DEFAULT_EXCEPTION_MSG = "SQL Exception"; private static final String DEFAULT_EXCEPTION_MSG = "SQL Exception";
private static final String DEFAULT_WARNING_MSG = "SQL Warning"; private static final String DEFAULT_WARNING_MSG = "SQL Warning";
private final boolean logWarnings;
private static final SQLExceptionConverter DEFAULT_CONVERTER = new SQLStateConverter( private static final SQLExceptionConverter DEFAULT_CONVERTER = new SQLStateConverter(
new ViolatedConstraintNameExtracter() { new ViolatedConstraintNameExtracter() {
@ -48,8 +49,8 @@ public String extractConstraintName(SQLException e) {
/** /**
* Create an exception helper with a default exception converter. * Create an exception helper with a default exception converter.
*/ */
public SqlExceptionHelper() { public SqlExceptionHelper( boolean logWarnings) {
sqlExceptionConverter = DEFAULT_CONVERTER; this( DEFAULT_CONVERTER, logWarnings );
} }
/** /**
@ -57,8 +58,9 @@ public SqlExceptionHelper() {
* *
* @param sqlExceptionConverter The exception converter to use. * @param sqlExceptionConverter The exception converter to use.
*/ */
public SqlExceptionHelper(SQLExceptionConverter sqlExceptionConverter) { public SqlExceptionHelper(SQLExceptionConverter sqlExceptionConverter, boolean logWarnings) {
this.sqlExceptionConverter = sqlExceptionConverter; this.sqlExceptionConverter = sqlExceptionConverter;
this.logWarnings = logWarnings;
} }
/** /**
@ -271,8 +273,10 @@ public void handleAndClearWarnings(
Connection connection, Connection connection,
WarningHandler handler) { WarningHandler handler) {
try { try {
if ( logWarnings ) {
walkWarnings( connection.getWarnings(), handler ); walkWarnings( connection.getWarnings(), handler );
} }
}
catch (SQLException sqle) { catch (SQLException sqle) {
// workaround for WebLogic // workaround for WebLogic
LOG.debug( "could not log warnings", sqle ); LOG.debug( "could not log warnings", sqle );
@ -300,7 +304,7 @@ public void handleAndClearWarnings(
WarningHandler handler) { WarningHandler handler) {
// See HHH-9174. Statement#getWarnings can be an expensive call for many JDBC libs. Don't do it unless // 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. // the log level would actually allow a warning to be logged.
if ( LOG.isEnabled( Level.WARN ) ) { if ( logWarnings ) {
try { try {
walkWarnings( statement.getWarnings(), handler ); walkWarnings( statement.getWarnings(), handler );
} }

View File

@ -27,7 +27,7 @@
public class Expectations { public class Expectations {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( Expectations.class ); 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_EXPECTED_COUNT = 1;
public static final int USUAL_PARAM_POSITION = 1; public static final int USUAL_PARAM_POSITION = 1;

View File

@ -12,7 +12,9 @@
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment; 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.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
@ -67,7 +69,12 @@ public void release() throws SQLException {
private void releaseConnection() throws SQLException { private void releaseConnection() throws SQLException {
if ( connection != null ) { if ( connection != null ) {
try { try {
new SqlExceptionHelper().logAndClearWarnings( connection ); final boolean logWarning = ConfigurationHelper.getBoolean(
AvailableSettings.LOG_JDBC_WARNINGS,
cfgProperties,
false
);
new SqlExceptionHelper( logWarning ).logAndClearWarnings( connection );
} }
finally { finally {
try { try {

View File

@ -24,9 +24,11 @@
class SuppliedConnectionHelper implements ConnectionHelper { class SuppliedConnectionHelper implements ConnectionHelper {
private Connection connection; private Connection connection;
private boolean toggleAutoCommit; private boolean toggleAutoCommit;
private final SqlExceptionHelper sqlExceptionHelper;
public SuppliedConnectionHelper(Connection connection) { public SuppliedConnectionHelper(Connection connection, SqlExceptionHelper sqlExceptionHelper) {
this.connection = connection; this.connection = connection;
this.sqlExceptionHelper = sqlExceptionHelper;
} }
public void prepare(boolean needsAutoCommit) throws SQLException { public void prepare(boolean needsAutoCommit) throws SQLException {
@ -47,7 +49,7 @@ public Connection getConnection() {
} }
public void release() throws SQLException { public void release() throws SQLException {
new SqlExceptionHelper().logAndClearWarnings( connection ); sqlExceptionHelper.logAndClearWarnings( connection );
if ( toggleAutoCommit ) { if ( toggleAutoCommit ) {
connection.setAutoCommit( false ); connection.setAutoCommit( false );
} }

View File

@ -28,9 +28,11 @@ class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper {
private ConnectionProvider provider; private ConnectionProvider provider;
private Connection connection; private Connection connection;
private boolean toggleAutoCommit; private boolean toggleAutoCommit;
private final SqlExceptionHelper sqlExceptionHelper;
public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) { public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider, SqlExceptionHelper sqlExceptionHelper) {
this.provider = provider; this.provider = provider;
this.sqlExceptionHelper = sqlExceptionHelper;
} }
public void prepare(boolean needsAutoCommit) throws SQLException { public void prepare(boolean needsAutoCommit) throws SQLException {
@ -54,7 +56,7 @@ public Connection getConnection() throws SQLException {
public void release() throws SQLException { public void release() throws SQLException {
// we only release the connection // we only release the connection
if ( connection != null ) { if ( connection != null ) {
new SqlExceptionHelper().logAndClearWarnings( connection ); sqlExceptionHelper.logAndClearWarnings( connection );
if ( toggleAutoCommit ) { if ( toggleAutoCommit ) {
connection.setAutoCommit( false ); connection.setAutoCommit( false );
} }

View File

@ -15,6 +15,7 @@
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver; 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.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
@ -122,6 +123,7 @@ GenerationTarget[] buildGenerationTargets(
jdbcContext.getSqlStatementLogger(), jdbcContext.getSqlStatementLogger(),
needsAutoCommit needsAutoCommit
) )
, serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper()
); );
} }
@ -153,7 +155,8 @@ GenerationTarget[] buildGenerationTargets(
} }
if ( targetDescriptor.getTargetTypes().contains( TargetType.DATABASE ) ) { if ( targetDescriptor.getTargetTypes().contains( TargetType.DATABASE ) ) {
targets[index] = new GenerationTargetToDatabase( connectionContext ); targets[index] = new GenerationTargetToDatabase( connectionContext
, serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper() );
} }
return targets; return targets;

View File

@ -453,6 +453,7 @@ public void doDrop(
jdbcContext.getSqlStatementLogger(), jdbcContext.getSqlStatementLogger(),
true true
) )
, serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper()
) )
}; };
} }
@ -535,6 +536,7 @@ public void perform(ServiceRegistry serviceRegistry) {
serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(), serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger(),
true true
) )
, serviceRegistry.getService( JdbcEnvironment.class ).getSqlExceptionHelper()
); );
target.prepare(); target.prepare();
try { try {

View File

@ -24,13 +24,18 @@
public class GenerationTargetToDatabase implements GenerationTarget { public class GenerationTargetToDatabase implements GenerationTarget {
private static final CoreMessageLogger log = CoreLogging.messageLogger( GenerationTargetToDatabase.class ); private static final CoreMessageLogger log = CoreLogging.messageLogger( GenerationTargetToDatabase.class );
private final SqlExceptionHelper sqlExceptionHelper = new SqlExceptionHelper(); private final SqlExceptionHelper sqlExceptionHelper;
private final JdbcConnectionContext jdbcConnectionContext; private final JdbcConnectionContext jdbcConnectionContext;
private Statement jdbcStatement; private Statement jdbcStatement;
public GenerationTargetToDatabase(JdbcConnectionContext jdbcConnectionContext) { public GenerationTargetToDatabase(JdbcConnectionContext jdbcConnectionContext) {
this( jdbcConnectionContext, new SqlExceptionHelper( true ) );
}
public GenerationTargetToDatabase(JdbcConnectionContext jdbcConnectionContext, SqlExceptionHelper sqlExceptionHelper) {
this.jdbcConnectionContext = jdbcConnectionContext; this.jdbcConnectionContext = jdbcConnectionContext;
this.sqlExceptionHelper = sqlExceptionHelper;
} }
@Override @Override