HHH-12392 - Caching SchemaResolver delegate with multiple data sources

This commit is contained in:
Andrea Boriero 2018-03-15 12:09:47 +00:00
parent 3a2ab40314
commit 00be50331f
1 changed files with 22 additions and 21 deletions

View File

@ -34,34 +34,35 @@ public class DefaultSchemaNameResolver implements SchemaNameResolver {
} }
private void determineAppropriateResolverDelegate(Connection connection) { private void determineAppropriateResolverDelegate(Connection connection) {
if ( delegate == null ) { // unfortunately Connection#getSchema is only available in Java 1.7 and above
// unfortunately Connection#getSchema is only available in Java 1.7 and above // and Hibernate still baselines on 1.6. So for now, use reflection and
// and Hibernate still baselines on 1.6. So for now, use reflection and // leverage the Connection#getSchema method if it is available.
// leverage the Connection#getSchema method if it is available. try {
try { final Class<? extends Connection> jdbcConnectionClass = connection.getClass();
final Class<? extends Connection> jdbcConnectionClass = connection.getClass(); final Method getSchemaMethod = jdbcConnectionClass.getMethod( "getSchema" );
final Method getSchemaMethod = jdbcConnectionClass.getMethod( "getSchema" ); if ( getSchemaMethod != null && getSchemaMethod.getReturnType().equals( String.class ) ) {
if ( getSchemaMethod != null && getSchemaMethod.getReturnType().equals( String.class ) ) { try {
try { // If the JDBC driver does not implement the Java 7 spec, but the JRE is Java 7
// If the JDBC driver does not implement the Java 7 spec, but the JRE is Java 7 // then the getSchemaMethod is not null but the call to getSchema() throws an java.lang.AbstractMethodError
// then the getSchemaMethod is not null but the call to getSchema() throws an java.lang.AbstractMethodError connection.getSchema();
connection.getSchema(); delegate = new SchemaNameResolverJava17Delegate();
delegate = new SchemaNameResolverJava17Delegate( );
}
catch (java.lang.AbstractMethodError e) {
log.debugf( "Unable to use Java 1.7 Connection#getSchema" );
delegate = SchemaNameResolverFallbackDelegate.INSTANCE;
}
} }
else { catch (java.lang.AbstractMethodError e) {
log.debugf( "Unable to use Java 1.7 Connection#getSchema" ); log.debugf( "Unable to use Java 1.7 Connection#getSchema" );
delegate = SchemaNameResolverFallbackDelegate.INSTANCE; delegate = SchemaNameResolverFallbackDelegate.INSTANCE;
} }
} }
catch (Exception ignore) { else {
log.debugf( "Unable to resolve connection default schema : " + ignore.getMessage() ); log.debugf( "Unable to use Java 1.7 Connection#getSchema" );
delegate = SchemaNameResolverFallbackDelegate.INSTANCE;
} }
} }
catch (Exception ignore) {
log.debugf(
"Unable to use Java 1.7 Connection#getSchema : An error occurred trying to resolve the connection default schema resolver: "
+ ignore.getMessage() );
delegate = SchemaNameResolverFallbackDelegate.INSTANCE;
}
} }
@Override @Override