0

I am an facing error in weblogic:

java.lang.ClassCastException: weblogic.jdbc.wrapper.Array_oracle_sql_ARRAY cannot be cast to oracle.sql.ARRAY at weblogic.jdbc.wrapper.CallableStatement_oracle_jdbc_driver_OracleCallableStatementWrapper.getARRAY(Unknown Source)

Code:

   public String[] methodName(String[] P1,String P2,String P3,String P4, String P5,int Sessioninfo)
{
    Connection conn = null;
    CallableStatement cstmt = null;
    ResultSet rs = null;
    String[] returnArray = null;

    try {
        ds=getDataSource(Sessioninfo);
        conn = ds.getConnection();
        conn = new CommonsDbcpNativeJdbcExtractor().getNativeConnection(conn);
        conn.setAutoCommit(false);

        ArrayDescriptor oracleVarchar2Collection =
                ArrayDescriptor.createDescriptor("VARRAY_PARTS",conn);
        ARRAY sqlNos = new ARRAY(oracleVarchar2Collection, conn, P1);

        cstmt =conn.prepareCall("{call Procedure1(?,?,?,?,?,?)}");
        cstmt.setObject(1, sqlNos);
        cstmt.setString(2, P2);
        cstmt.setString(3, WebpartsUtility.convertSQLStringIN(P3,","));
        cstmt.setString(4, WebpartsUtility.convertSQLStringIN(P4,","));
        cstmt.setString(5,P5);

        cstmt.registerOutParameter(6,OracleTypes.ARRAY, "VARRAY_PARTS");

        cstmt.setFetchSize(2500);
        cstmt.execute();
        ARRAY mainArray = ((OracleCallableStatement)cstmt).getARRAY(6);
        returnArray = (String[]) mainArray.getArray();

    } catch (SQLException ex) {
        logger.fatalMsg("Sql Exception Occured :: "+ex.getMessage(),ex);
    } catch (Exception ex) {
        logger.fatalMsg("Exception Occured :: "+ex.getMessage(),ex);
    } finally {
        try {
            if (cstmt != null) {
                cstmt.close();
                cstmt = null;
            }                       
            if (conn != null) {
                conn.close();
                conn = null;
            }
            if (ds != null) {
                ds = null;
            }
        } catch (SQLException ex) {
            logger.fatalMsg("Sql Exception Occured :: "+ex.getMessage(),ex);
        } catch (Exception ex) {
            logger.fatalMsg("Exception Occured :: "+ex.getMessage(),ex);
        }
    }

    return returnArray;
}
Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
tomi
  • 21
  • 1
  • 5

3 Answers3

3

A simple parameter change in the datasource should make it work.

Go to datasource -> select the datasource -> Configuration -> Connection Pool -> Advanced then uncheck "Wrap Data Types".

This should resolve the issue.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Rajib Paul
  • 31
  • 2
1

Sometimes the connection is returned as a wrapper around the native connection and you need to unwrap it:

conn = new CommonsDbcpNativeJdbcExtractor().getNativeConnection(conn);
OracleConnection oConn;
if ( conn.isWrapperFor( OracleConnection.class ) )
{
  oConn = (OracleConnection) conn.unwrap( OracleConnection.class );
}
else
{
  oConn = (OracleConnection) conn;
}

Or, as per this answer, you could get the underlying callable statement (and not a wrapped statement):

OracleCallableStatement cstmt
    = (OracleCallableStatement) conn.prepareCall("{call Procedure1(?,?,?,?,?,?)}")
                                    .getUnderlyingStatement();

Another potential solution would be to not use intermediate variables and just do:

returnArray = (String[]) ((OracleCallableStatement)cstmt).getARRAY(6).getArray();

Update:

Based on this answer you could also try:

ARRAY mainArray = (ARRAY) ((weblogic.jdbc.wrapper.Array) (callableStmt).getObject(3))
                              .unwrap(ARRAY.class);
MT0
  • 143,790
  • 11
  • 59
  • 117
  • None of these worked in my case. Still facing the same error: weblogic.jdbc.wrapper.Array_oracle_sql_ARRAY cannot be cast to oracle.sql.ARRAY – tomi Aug 28 '17 at 11:32
1

The issue got resolved adding the below code:

           try {
        statement.setLong(1, new Long(1));
        statement.registerOutParameter(2, Types.ARRAY, "TNUMBERTABLE");
        statement.execute();
        ARRAY ar = null;
        Object someArray = statement.getArray(2);
        if (someArray instanceof weblogic.jdbc.wrapper.Array)
            ar =
      (oracle.sql.ARRAY(((weblogic.jdbc.wrapper.Array)someArray).unwrap(Class.forName("oracle.sql.ARRAY")));
        else
            ar = (oracle.sql.ARRAY)someArray;

        for (long i : ar.getLongArray()) {
            //code if needed
        }

    } 

You can check this link:http://adfpractice-fedor.blogspot.com/2011/09/weblogic-wrapping-data-types.html

tomi
  • 21
  • 1
  • 5