簡體   English   中英

當需要nativeJdbcExtractor時,Spring 5 JDBC方法是什么?

[英]What is the Spring 5 JDBC approach when nativeJdbcExtractor is needed?

我剛剛升級了Spring / SpringBoot依賴項,並注意到類JdbcTemplate不再具有屬性“nativeJdbcExtractor”。

我能夠找到詳細信息和背景: https//jira.spring.io/browse/SPR-14670

但是我無法找到替換配置。 我使用commons-dbcp庫和Spring類如SimpleJdbcCall等。我從不處理低級JDBC API,但是如果供應商代碼需要其真正的連接類型(Oracle), nativeJdbcExtractor設置確保它將在Spring JDBC代碼深處得到它(不是我的應用程序代碼)。 我不知道如何通過調用connection.unwrap()來解決這個問題,如果我需要Spring API來自動處理它,就像它過去那樣。

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

新:

看起來你正在使用spring-data-jdbc-ext org.springframework.data.jdbc.support.oracle.SqlArrayValue 錯誤就在那里,那里應該發生解纏。

類似以下內容(未經測試)

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而是您的代碼正在進行轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM