首页 > 解决方案 > 当需要 nativeJdbcExtractor 时,Spring 5 JDBC 方法是什么?

问题描述

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

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

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

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

那是隐藏在 DataSource 配置中的某个地方吗?我已经从 commons-dbcp 1.4 升级到 commons-dbcp2 但到目前为止找不到任何有用的东西(BasicDataSource)。

更新:以下线程是相关的,但我无法消化我正在寻找的答案,因为 Connection 对象是在 JdbcTemplate 类中获得的,因此不受我的控制。

在 Spring 5 中替换 jdbc.support.nativejdbc remove

更新 #2 - 堆栈跟踪

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

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

    public void setPhysicalConnectionOf(Connection var1) {
    this.connection = ((oracle.jdbc.OracleConnection)var1).physicalConnectionWithin();
}

标签: javaspring-bootjdbcspring-jdbcapache-commons-dbcp

解决方案


新的:

看起来你正在使用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);
}

关于 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;

称呼

OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);

在堆栈跟踪中,您会看到谁在进行强制转换,这将在您的代码中,因为SimpleJdbcCall朋友们不区分大小写OracleConnection。问题不在于JdbcTemplate你的代码在做演员。


推荐阅读