当需要nativeJdbcExtractor时,Spring 5 JDBC方法是什么? [英] What is the Spring 5 JDBC approach when nativeJdbcExtractor is needed?

查看:1141
本文介绍了当需要nativeJdbcExtractor时,Spring 5 JDBC方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just upgraded Spring/SpringBoot dependencies and noticed that class JdbcTemplate does not have property "nativeJdbcExtractor" any more.

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

I was able to find the details and background: https://jira.spring.io/browse/SPR-14670

但是我找不到替换配置.我使用commons-dbcp库和诸如 SimpleJdbcCall 之类的Spring类.我从不处理低级JDBC API,但是,如果供应商代码需要其实际的连接类型(Oracle),则使用 nativeJdbcExtractor >设置确保它可以使它深入Spring JDBC代码(而不是我的应用程序代码)中.我不确定如何通过调用 connection.unwrap()来解决此问题,如果我需要Spring API像过去那样自动处理它.

However I was not able to find the replacement configuration. I use commons-dbcp library and Spring classes like SimpleJdbcCall etc. I never deal with low level JDBC API, however if the vendor code needs its real Connection type (Oracle) the nativeJdbcExtractor settings ensured it will get it somewhere deep in Spring JDBC code (not my app. code). I´m not sure how I can address this by calling connection.unwrap() if I need Spring API to handle this automatically as it did it in past.

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

这是否隐藏在DataSource配置中的某个位置?我已经从commons-dbcp 1.4升级到commons-dbcp2,但到目前为止找不到任何有用的信息(BasicDataSource).

Is that hidden somewhere in DataSource configuration? I have upgraded from commons-dbcp 1.4 to commons-dbcp2 but cannot find anything useful so far (BasicDataSource).

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

Update: Following thread is relevant but I cannot digest the answer I´m looking for since the Connection object is obtained within JdbcTemplate class and thus out of my control.

在春季5中删除jdbc.support.nativejdbc的替换

更新#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();
}

推荐答案

新功能:

您似乎正在使用spring-data-jdbc-ext中的org.springframework.data.jdbc.support.oracle.SqlArrayValue.错误在那里,展开应该在那里发生.

It looks like you're using org.springframework.data.jdbc.support.oracle.SqlArrayValue from spring-data-jdbc-ext. The bug is there and the unwrapping should happen there.

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

Something like the following (untested)

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

关于JDBC驱动程序:

Regarding the JDBC driver:

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

You are using Java 8 or later since Spring 5 requires Java 8. Therefore you should use ojdbc8 (8 means Java 8). I strongly recommend using the latest 12.2.0.1 driver from http://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html.

如果您从 http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_02 ,您将看到该潜水员也适用于较旧的数据库.

If you check the driver interoperability matrix from http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#01_02 you will see that this diver also works with older databases.

旧/过时:

在执行强制转换而不是强制调用#unwrap的代码中.因此,而不是

In your code that performs the cast instead of casting call #unwrap. So instead of

OracleConnection oracleConnection = (OracleConnection) connection;

致电

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

在堆栈跟踪中,您可以看到谁在执行强制转换,这将在您的代码中,因为SimpleJdbcCall和朋友不区分OracleConnection.问题不在JdbcTemplate中,而是您的代码进行了强制转换.

In the stack trace you see who is doing the cast, this will be in your code because SimpleJdbcCall and friends don't case to OracleConnection. The issue is not in JdbcTemplate but your code doing the cast.

这篇关于当需要nativeJdbcExtractor时,Spring 5 JDBC方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆