当需要nativeJdbcExtractor时,Spring 5 JDBC方法是什么?

Jav*_*ude 6 java jdbc spring-jdbc apache-commons-dbcp spring-boot

我刚刚升级了 Spring/SpringBoot 依赖项,并注意到 JdbcTemplate 类不再具有属性“nativeJdbcExtractor”。

\n\n

我能够找到详细信息和背景:\n https://jira.spring.io/browse/SPR-14670

\n\n

但是我无法找到替换配置。我使用 commons-dbcp 库和 Spring 类,如SimpleJdbcCall等。我从不处理低级 JDBC API,但是如果供应商代码需要其真正的连接类型(Oracle),nativeJdbcExtractor设置确保它将在 Spring JDBC 代码深处得到它(不是我的应用程序代码)。我\xc2\xb4m 不知道如何通过调用connection.unwrap()来解决这个问题,如果我需要Spring API 像过去那样自动处理这个问题。

\n\n

java.lang.ClassCastException:org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 oracle.jdbc.OracleConnection

\n\n

它是否隐藏在数据源配置中的某处?我已从 commons-dbcp 1.4 升级到 commons-dbcp2,但到目前为止找不到任何有用的东西(BasicDataSource)。

\n\n

更新:以下线程相关,但我无法消化 I\xc2\xb4m 寻找的答案,因为 Connection 对象是在 JdbcTemplate 类中获得的,因此超出了我的控制范围。

\n\n

替换 jdbc.support.nativejdbc 在 Spring 5 中删除

\n\n

更新 #2 - 堆栈跟踪

\n\n
Caused by: java.lang.ClassCastException: org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection\nat oracle.sql.TypeDescriptor.setPhysicalConnectionOf(TypeDescriptor.java:832)\nat oracle.sql.TypeDescriptor.<init>(TypeDescriptor.java:586)\nat oracle.sql.ArrayDescriptor.<init>(ArrayDescriptor.java:224)\nat org.springframework.data.jdbc.support.oracle.SqlArrayValue.createTypeValue(SqlArrayValue.java:90)\nat org.springframework.jdbc.core.support.AbstractSqlTypeValue.setTypeValue(AbstractSqlTypeValue.java:60)\nat org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:293)\nat org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:232)\nat org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:147)\nat org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:200)\nat org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1048)\nat org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1104)\nat org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:414)\nat org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:397)\nat org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:193)\n
Run Code Online (Sandbox Code Playgroud)\n\n

更新 #3 - 执行转换的代码 (Oracle JDBC)

\n\n
    public void setPhysicalConnectionOf(Connection var1) {\n    this.connection = ((oracle.jdbc.OracleConnection)var1).physicalConnectionWithin();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Phi*_*all 4

新的:

看来您正在使用org.springframework.data.jdbc.support.oracle.SqlArrayValuefrom spring-data-jdbc-ext. 错误就在那里,解包应该在那里发生。

类似于以下内容(未经测试)

protected Object createTypeValue(Connection conn, int sqlType, String typeName)
        throws SQLException { 
    return conn.unwrap(OracleConnection.class).createArray(typeName, values);
}
Run Code Online (Sandbox Code Playgroud)

关于 JDBC 驱动程序:

您正在使用 Java 8 或更高版本,因为 Spring 5 需要 Java 8。因此您应该使用 ojdbc8(8 表示 Java 8)。我强烈建议使用http://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html中的最新 12.2.0.1 驱动程序。

如果您从http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_02检查驱动程序互操作性矩阵,您将看到该驱动程序也适用于较旧的数据库。

旧的/过时的:

在执行强制转换而不是强制转换调用的代码中#unwrap。所以而不是

OracleConnection oracleConnection = (OracleConnection) connection;
Run Code Online (Sandbox Code Playgroud)

称呼

OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
Run Code Online (Sandbox Code Playgroud)

在堆栈跟踪中,您可以看到谁在进行转换,这将在您的代码中,因为SimpleJdbcCall朋友们不会这样做OracleConnection。问题不在于JdbcTemplate您执行强制转换的代码。