描述:没有提供spring的nativeJdbcExtractor实现导致在oracle数据库中插入blob类型数据时出现异常
com.ufgov.gk.common.system.exception.OtherException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; --- The error occurred in sqlmap/AsFile.xml. --- The error occurred while applying a parameter map. --- Check the AsFile.insertAsFile-InlineParameterMap. --- Check the parameter mapping for the 'fileContent' property. ---Cause: org.springframework.dao.InvalidDataAccessApiUsageException: OracleLobCreator needs to work on [oracle.jdbc.OracleConnection], not on [com.apusic.jdbc.adapter.ConnectionHandle]:specify a corresponding NativeJdbcExtractor; nested exception is java.lang.ClassCastException: com.apusic.jdbc.adapter.ConnectionHandle cannot be cast to oracle.jdbc.OracleConnection; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
检查数据库发现As_file表中file_content字段是一个blob字段,报错信息提示:
OracleLobCreator needs to work on [oracle.jdbc.OracleConnection], not on [com.apusic.jdbc.adapter.ConnectionHandle] com.apusic.jdbc.adapter.ConnectionHandle cannot be cast to oracle.jdbc.OracleConnection
问题原因:这个问题在spring框架中比较常见。对于blob和clob的字段存储,spring的NativeJdbcExtractor会根据数据源和AS类型的不同,提供不通的API接口,从oracle的本地jdbc的jar报包中抽取对象类,spring针对常用的应用服务器都有对应的NativeJdbcExtractor接口(如webshpere、weblogic jboss等),但是在apusic中并未提供处理接口。
解决方法:nativeJdbcExtractor是需要根据不同的应用服务器实现,因此需要apusic应用服务器提供自己的实现类,具体如下:
可以编译ApusicNativeJdbcExtractor.java ,此类是apusic对nativeJdbcExtractor的实现,具体参开代码如下:
package org.springframework.jdbc.support.nativejdbc;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.util.ReflectionUtils;
/**
* Implementation of the {@link NativeJdbcExtractor} interface for Apusic,
* supporting Apusic Application Server 6.0+.
*
* <p>Returns the underlying native Connection, Statement, etc to
* application code instead of Apusic' wrapper implementations.
* The returned JDBC classes can then safely be cast, e.g. to
* <code>oracle.jdbc.OracleConnection</code>.
*
* <p>This NativeJdbcExtractor can be set just to <i>allow</i> working with
* a Apusic connection pool: If a given object is not a Apusic wrapper,
* it will be returned as-is.
*
* @author weiyongsen
* @since 07.08.2011
* @see com.apusic.jdbc.adapter.ConnectionHandle#getActualConnection
* @see com.apusic.jdbc.adapter.StatementHandle#getActualStatement
* @see com.apusic.jdbc.adapter.ResultSetHandle#getActualResultSet
*/
public class ApusicNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
private static final String WRAPPED_CONNECTION_NAME = "com.apusic.jdbc.adapter.ConnectionHandle";
private static final String WRAPPED_STATEMENT_NAME = "com.apusic.jdbc.adapter.StatementHandle";
private static final String WRAPPED_RESULT_SET_NAME = "com.apusic.jdbc.adapter.ResultSetHandle";
private Class wrappedConnectionClass;
private Class wrappedStatementClass;
private Class wrappedResultSetClass;
private Method getUnderlyingConnectionMethod;
private Method getUnderlyingStatementMethod;
private Method getUnderlyingResultSetMethod;
public ApusicNativeJdbcExtractor() {
try {
this.wrappedConnectionClass = getClass().getClassLoader().loadClass(WRAPPED_CONNECTION_NAME);
this.wrappedStatementClass = getClass().getClassLoader().loadClass(WRAPPED_STATEMENT_NAME);
this.wrappedResultSetClass = getClass().getClassLoader().loadClass(WRAPPED_RESULT_SET_NAME);
this.getUnderlyingConnectionMethod =
this.wrappedConnectionClass.getMethod("getActualConnection", (Class[]) null);
this.getUnderlyingStatementMethod =
this.wrappedStatementClass.getMethod("getActualStatement", (Class[]) null);
this.getUnderlyingResultSetMethod =
this.wrappedResultSetClass.getMethod("getActualResultSet", (Class[]) null);
}
catch (Exception ex) {
throw new IllegalStateException(
"Could not initialize ApusicNativeJdbcExtractor because Apusic API classes are not available: " + ex);
}
}
protected Connection doGetNativeConnection(Connection con) throws SQLException {
if (this.wrappedConnectionClass.isAssignableFrom(con.getClass())) {
return (Connection) ReflectionUtils.invokeMethod(this.getUnderlyingConnectionMethod, con);
}
return con;
}
public Statement getNativeStatement(Statement stmt) throws SQLException {
if (this.wrappedStatementClass.isAssignableFrom(stmt.getClass())) {
return (Statement) ReflectionUtils.invokeMethod(this.getUnderlyingStatementMethod, stmt);
}
return stmt;
}
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
return (PreparedStatement) getNativeStatement(ps);
}
public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException {
return (CallableStatement) getNativeStatement(cs);
}
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
if (this.wrappedResultSetClass.isAssignableFrom(rs.getClass())) {
return (ResultSet) ReflectionUtils.invokeMethod(this.getUnderlyingResultSetMethod, rs);
}
return rs;
}
}
放到应用目录的类目录,修改应用对应的nativeJdbcExtractor配置即可。