HikariCP pass Oracle custom type

若如初见. 提交于 2019-12-10 09:28:52

问题


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

I get a java.lang.ClassCastException - com.zaxxer.hikari.pool.HikariProxyConnection cannot be cast to oracle.jdbc.OracleConnection.

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


回答1:


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

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



来源:https://stackoverflow.com/questions/40087536/hikaricp-pass-oracle-custom-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!