Java.lang.ClassCastException: oracle.sql.ARRAY cannot be cast to oracle.sql.ARRAY

arraysjavaoracle

I have created a stored procedure in Oracle with tabletype parameter like bellow

  create or replace PROCEDURE CREATENEWPrj
(
          Name IN VARCHAR2,
          countryVal IN VARCHAR2,
          outingDetailsInfo IN  TBLSHIPPINGINFO
)
AS
 ......
  .....

Trying to call procedure from my java code like bellow

        Gson gson=new Gson();

        Type listType = new TypeToken<List<ShippingInfo>>(){}.getType();
        List<ShippingInfo> shippingInfo= gson.fromJson(requestObj.getString("shipping"),listType);

        try
        {
            call = app.databaseHelper.preparedCall("CREATENEWPrj(?,?,?)");
            call.setString(1,requestObj.getString("Name"));
            call.setString(2,requestObj.getString("countryVal"));

            Driver myDriver = new oracle.jdbc.driver.OracleDriver();
            DriverManager.registerDriver( myDriver );

            Connection conn = DriverManager.getConnection(URL, USER, PASS);



            STRUCT[] structs = new STRUCT[shippingInfo.size()];
            StructDescriptor structDescriptor =
                    StructDescriptor.createDescriptor("SHIPPINGINFO",conn);

            for (int i = 0; i < shippingInfo.size(); ++i) {

                ShippingInfo str =
                        shippingInfo.get(i);
                Object[] objects = new Object[] { str.getCarrierId(),str.getRouteId()};

                STRUCT struct =new STRUCT(structDescriptor, conn, objects);
                structs[i] = struct;
            }
            ArrayDescriptor arrayDescriptor =ArrayDescriptor.createDescriptor("TBLSHIPPINGINFO",conn);

            ARRAY arraypass = new ARRAY(arrayDescriptor, conn, structs);

            call.setArray(3,  arraypass);

            call.execute();
    }

But Getting Exception "java.lang.ClassCastException: oracle.sql.ARRAY cannot be cast to oracle.sql.ARRAY" from `call.setArray(24, arraypass);

oracle.sql.ARRAY.class.getProtectionDomain().getCodeSource().getLocation()
and
arraypass.getClass().getProtectionDomain().getCodeSource().getLocation()
Both are equal

Checked "oracle.sql.ARRAY.class.getClassLoader().equals(array_pass.getClass().getClassLo‌​ader());" ,its also returning True

Please guide me to solve this issue.

Best Answer

You have the driver loaded multiple times. I saw this often in tomcat who share contexts like this:

  • Tomcat -URLClassLoader without ojdbc.jar
    • Webapp1 - URLClassLoader with webapp1/libs/ojdbc.jar
    • Webapp2 - URLClassLoader with webapp2/libs/ojdbc.jar

Now, if you offer a instance of oracle.sql.ARRAY from Webapp1 to Webapp2 this ClassCastException occours.

The solution is to load the ojdbc only once like this:

  • Tomcat -URLClassLoader with one and only ojdbc.jar
    • Webapp1 - URLClassLoader without ojdbc.jar
    • Webapp2 - URLClassLoader without ojdbc.jar