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";
/**
* 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 @@ 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;

View File

@ -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<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_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 );
}

View File

@ -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;

View File

@ -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 {

View File

@ -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 );
}

View File

@ -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 );
}

View File

@ -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;

View File

@ -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 {

View File

@ -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