Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HikariCP pass Oracle custom type

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?

like image 786
0bj3ct Avatar asked Oct 17 '16 13:10

0bj3ct


1 Answers

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 !

like image 148
Nitin Avatar answered Sep 20 '22 10:09

Nitin