OracleTableMetaDataProvider tries Connection.unwrap as well
Issue: SPR-14670
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,7 +30,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
/**
|
||||
* Oracle-specific implementation of the {@link org.springframework.jdbc.core.metadata.TableMetaDataProvider}.
|
||||
* Supports a feature for including synonyms in the metadata lookup. Also supports lookup of current schema
|
||||
* using the sys_context.
|
||||
* using the {@code sys_context}.
|
||||
*
|
||||
* <p>Thanks to Mike Youngstrom and Bruce Campbell for submitting the original suggestion for the Oracle
|
||||
* current schema lookup implementation.
|
||||
@@ -46,17 +46,53 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||
private String defaultSchema;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor used to initialize with provided database metadata.
|
||||
* @param databaseMetaData metadata to be used
|
||||
*/
|
||||
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLException {
|
||||
this(databaseMetaData, false);
|
||||
}
|
||||
|
||||
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData, boolean includeSynonyms) throws SQLException {
|
||||
/**
|
||||
* Constructor used to initialize with provided database metadata.
|
||||
* @param databaseMetaData metadata to be used
|
||||
* @param includeSynonyms whether to include synonyms
|
||||
*/
|
||||
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData, boolean includeSynonyms)
|
||||
throws SQLException {
|
||||
|
||||
super(databaseMetaData);
|
||||
this.includeSynonyms = includeSynonyms;
|
||||
|
||||
lookupDefaultSchema(databaseMetaData);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Oracle-based implementation for detecting the current schema.
|
||||
*/
|
||||
private void lookupDefaultSchema(DatabaseMetaData databaseMetaData) {
|
||||
try {
|
||||
CallableStatement cstmt = null;
|
||||
try {
|
||||
cstmt = databaseMetaData.getConnection().prepareCall(
|
||||
"{? = call sys_context('USERENV', 'CURRENT_SCHEMA')}");
|
||||
cstmt.registerOutParameter(1, Types.VARCHAR);
|
||||
cstmt.execute();
|
||||
this.defaultSchema = cstmt.getString(1);
|
||||
}
|
||||
finally {
|
||||
if (cstmt != null) {
|
||||
cstmt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
logger.debug("Encountered exception during default schema lookup", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultSchema() {
|
||||
if (this.defaultSchema != null) {
|
||||
@@ -65,6 +101,7 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||
return super.getDefaultSchema();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void initializeWithTableColumnMetaData(DatabaseMetaData databaseMetaData,
|
||||
String catalogName, String schemaName, String tableName) throws SQLException {
|
||||
@@ -80,21 +117,29 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||
if (nativeJdbcExtractor != null) {
|
||||
con = nativeJdbcExtractor.getNativeConnection(con);
|
||||
}
|
||||
boolean isOracleCon;
|
||||
|
||||
boolean isOracleCon = false;
|
||||
try {
|
||||
Class<?> oracleConClass = con.getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
|
||||
isOracleCon = oracleConClass.isInstance(con);
|
||||
if (!isOracleCon) {
|
||||
con = (Connection) con.unwrap(oracleConClass);
|
||||
isOracleCon = oracleConClass.isInstance(con);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Couldn't find Oracle JDBC API: " + ex);
|
||||
logger.info("Could not find Oracle JDBC API: " + ex);
|
||||
}
|
||||
isOracleCon = false;
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
// No OracleConnection found by unwrap
|
||||
}
|
||||
|
||||
if (!isOracleCon) {
|
||||
logger.warn("Unable to include synonyms in table metadata lookup. Connection used for " +
|
||||
"DatabaseMetaData is not recognized as an Oracle connection: " + con);
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Unable to include synonyms in table metadata lookup - no Oracle Connection: " + con);
|
||||
}
|
||||
super.initializeWithTableColumnMetaData(databaseMetaData, catalogName, schemaName, tableName);
|
||||
return;
|
||||
}
|
||||
@@ -112,8 +157,8 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||
ReflectionUtils.makeAccessible(setIncludeSynonyms);
|
||||
setIncludeSynonyms.invoke(con, Boolean.TRUE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidDataAccessApiUsageException("Couldn't prepare Oracle Connection", ex);
|
||||
catch (Throwable ex) {
|
||||
throw new InvalidDataAccessApiUsageException("Could not prepare Oracle Connection", ex);
|
||||
}
|
||||
|
||||
super.initializeWithTableColumnMetaData(databaseMetaData, catalogName, schemaName, tableName);
|
||||
@@ -121,30 +166,8 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||
try {
|
||||
setIncludeSynonyms.invoke(con, originalValueForIncludeSynonyms);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidDataAccessApiUsageException("Couldn't reset Oracle Connection", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Oracle-based implementation for detecting the current schema.
|
||||
*/
|
||||
private void lookupDefaultSchema(DatabaseMetaData databaseMetaData) {
|
||||
try {
|
||||
CallableStatement cstmt = null;
|
||||
try {
|
||||
cstmt = databaseMetaData.getConnection().prepareCall("{? = call sys_context('USERENV', 'CURRENT_SCHEMA')}");
|
||||
cstmt.registerOutParameter(1, Types.VARCHAR);
|
||||
cstmt.execute();
|
||||
this.defaultSchema = cstmt.getString(1);
|
||||
}
|
||||
finally {
|
||||
if (cstmt != null) {
|
||||
cstmt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
catch (Throwable ex) {
|
||||
throw new InvalidDataAccessApiUsageException("Could not reset Oracle Connection", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user