HikariCP传递Oracle自定义类型 [英] HikariCP pass Oracle custom type

查看:257
本文介绍了HikariCP传递Oracle自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Oracle默认数据源切换到HikariCP.有一段代码将自定义Oracle类型传递给存储的参数,并将java.sql.Connection强制转换为oracle.jdbc.OracleConnection.

I switched to HikariCP from Oracle default datasource. There is a piece of code where I pass custom Oracle type to a stored parameter, and cast java.sql.Connection to oracle.jdbc.OracleConnection.

try(OracleConnection connection = (OracleConnection) dbConnect.getConnection()) {
        try(CallableStatement callableStatement = connection.prepareCall("{? = call pkg_applications.add_application(?,?,?)}")) {
            callableStatement.registerOutParameter(1, Types.VARCHAR);
            callableStatement.setString(2, form.getPolicyNumber());
            callableStatement.setString(3, form.getPolicyStart());

            Object[][] uploads = new Object[wrappers.size()][];

            for(int i=0; i<wrappers.size(); i++) {
                uploads[i] = new Object[4];
                uploads[i][0] = wrappers.get(i).getName();
                uploads[i][1] = wrappers.get(i).getFile().getContentType();
                uploads[i][2] = wrappers.get(i).getFile().getSize();
                uploads[i][3] = wrappers.get(i).getLocation();
            }

            callableStatement.setArray(4, connection.createARRAY("T_UPLOAD_FILE_TABLE", uploads));

            callableStatement.execute();
            int applicationId = callableStatement.getInt(1);

            operationResponse.setData(applicationId);
            operationResponse.setCode(ResultCode.OK);
        }
    }
    catch(Exception e) {
        log.error(e.getMessage(), e);
    }

我得到一个java.lang.ClassCastException - com.zaxxer.hikari.pool.HikariProxyConnection cannot be cast to oracle.jdbc.OracleConnection.

如何使用HikariCP将Oracle自定义类型传递给存储过程?

How can I pass Oracle custom types to a stored procedure using HikariCP?

推荐答案

从池中获得的是代理连接. 要访问基础的Oracle连接,应将unwrap()与isWrapperFor()结合使用:

What you get from pool is a proxy connection. To access the underlying Oracle connection, you should use unwrap() with isWrapperFor():

try (Connection hikariCon = dbConnect.getConnection()) {
   if (hikariCon.isWrapperFor(OracleConnection.class)) {
      OracleConnection connection = hikariCon.unwrap(OracleConnection.class);
      :
      :
   }

但是,示例中的OracleConnection特定于哪种方法?您可能根本不需要投射!

However, which method is OracleConnection specific in your example ? you may not need to cast at all !

这篇关于HikariCP传递Oracle自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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