Retrieving Native Connection from JBOSS wrapped Connection

我只是一个虾纸丫 提交于 2019-12-10 13:15:44

问题


I need to pass an array Object to Oracle 11 DB. I am using annotations based Spring 3.1 and using SimpleJdbcCall to call the procedure on JBOSS server. Here is the relevant jdbcCall.

SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
                              .withoutProcedureColumnMetaDataAccess()
                              .withProcedureName(IbeginDataBaseConstants.PROCEDURE_CREATE_NEW_ADMIN.VAL)
                              .declareParameters(new SqlParameter("inUserEmpID", OracleTypes.INTEGER))
                              .declareParameters(new SqlParameter("inCountryIDs", OracleTypes.ARRAY, "ibo_number_array" ))
                              .declareParameters(new SqlParameter("inSysRoleID", OracleTypes.INTEGER))
                              .declareParameters(new SqlParameter("inLoggedIn", OracleTypes.INTEGER))
                              .declareParameters(new SqlOutParameter("outStatus", OracleTypes.CHAR))
                              .declareParameters(new SqlOutParameter("outMsg", OracleTypes.VARCHAR));

As you can see inCountryIDsis an array that I need to send.

With help from google, I was able to get multiple ways through which I can send Array to DB. Here is the first one.

SqlTypeValue value = new AbstractSqlTypeValue() {
      protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
        ArrayDescriptor arrayDescriptor = new ArrayDescriptor(typeName, conn);
        ARRAY idArray = new ARRAY(arrayDescriptor, conn, ids);
        return idArray;
      }

And I added it to parameter source using

            sourceMap.addValue("inUserEmpID", newAdmin.getEmpId());
    sourceMap.addValue("inCountryIDs", value);

    sourceMap.addValue("inSysRoleID", newAdmin.getRoleId());
    sourceMap.addValue("inLoggedIn", newAdmin.getLoggedInId());

And the Exception that I got was org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to oracle.jdbc.OracleConnection

I understood the reason, SqlTypeValue implemention requires a OracleConnection whereas Spring passes WrappedConnection.

So, I tried to unwrap the connection using WrappedConnection class with this code.

WrappedConnection wrappedConnection = conn.unwrap(WrappedConnection.class);

But here the exception was

Not a wrapper for: org.jboss.jca.adapters.jdbc.WrappedConnection

In another try, I tried to cast existing connection to WrappedConnection using explicit cast with this.

WrappedConnection wrappedConn = (WrappedConnection)conn;

And still no luck. Exception.

org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to org.jboss.jca.adapters.jdbc.WrappedConnection

Well, All I had to do is to retrieve the underlying connection, so I tried to cast it into WrappedConnectionJDK6 so that I would be able to call the relevent method there.

WrappedConnectionJDK6 wrappedConnection = (WrappedConnectionJDK6)conn;

leads to org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6

And the second approach

Map in = Collections.singletonMap("inCountryIDs", new SqlArrayValue(idsArray));

The problem with this approach is that Collections.singletonMap returns an immutable map. So, I can't use it to add more parameters.

Is there any way through which I can retrieve the underlying connection in JBoss Wrapped Connection ? Or is there any other mechanism through which I can pass an array paramter without having to interact with connection ?

来源:https://stackoverflow.com/questions/24384252/retrieving-native-connection-from-jboss-wrapped-connection

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