Forum Stats

  • 3,782,584 Users
  • 2,254,669 Discussions
  • 7,880,131 Comments

Discussions

Cannot cast from ConnectionWrapper to oracle.jdbc.OracleConnection

2994778
2994778 Member Posts: 3

Hello Community,

I have an problem to get an Custom object from my Database.

Exception:

Java.lang.ClassCastException
org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
cannot be cast to oracle.jdbc.OracleConnection at
oracle.sql.scruptdescriptor.createDescriptor()

My Code:

Class.forName("oracle.jdbc.OracleDriver");
        Context contextEnv = (Context) new InitialContext().lookup("java:/comp/env");
        DataSource dataSource = (DataSource) contextEnv.lookup("xxxx/xxx/xxxx_xxx");
     
        Connection connectionDB = dataSource.getConnection();
  
       
        System.out.println("-- 1 --");
        System.out.println("Type: " + connectionDB.unwrap(oracle.jdbc.OracleConnection.class));
     
       
        final StructDescriptor structDescriptor_Mitglied = StructDescriptor.createDescriptor("KPZ.RECTYPE_MITGLIED", connectionDB);   
        final ResultSetMetaData metaData_Mitglieder = structDescriptor_Mitglied.getMetaData();
        System.out.println("-- 2 --");
      
         Struct recMitglied = connectionDB.createStruct("KPZ.RECTYPE_MITGLIED",new Object[]
                                             {
                                                 itemMitglied.getItemProperty("bname_id").getValue()
                                                ,itemMitglied.getItemProperty("bvorgang_id").getValue()
                                                ,itemMitglied.getItemProperty("mnr").getValue()
                                                ,itemMitglied.getItemProperty("bnr").getValue()              
                                                ,itemMitglied.getItemProperty("anrede").getValue()
                                                ,itemMitglied.getItemProperty("titel").getValue()
                                                ,itemMitglied.getItemProperty("vorname").getValue()
                                                ,itemMitglied.getItemProperty("nachname").getValue()
                                                ,itemMitglied.getItemProperty("name1").getValue()
                                                ,itemMitglied.getItemProperty("name2").getValue()
                                                ,itemMitglied.getItemProperty("name3").getValue()
                                                ,itemMitglied.getItemProperty("strasse").getValue()
                                                ,itemMitglied.getItemProperty("haus_nr").getValue()
                                                ,itemMitglied.getItemProperty("plz").getValue()
                                                ,itemMitglied.getItemProperty("ort").getValue()
                                                ,itemMitglied.getItemProperty("telefon").getValue()
                                                ,itemMitglied.getItemProperty("email").getValue()
                                                ,itemMitglied.getItemProperty("status_kurz").getValue()
                                             });
                 System.out.println("-- 3 --");
                                           
       
         CallableStatement cs;   
         cs = connectionDB.prepareCall("BEGIN KPZ.PCK_MITGLIED.PRC_LESE_MITGL_ADRESSE(?,?,?); END;");
        
       
        cs.setObject(1, recMitglied);       
        cs.registerOutParameter(2, Types.STRUCT,"KPZ.RECTYPE_TECHINFOS");    
        cs.registerOutParameter(3, Types.ARRAY,"KPZ.TABTYPE_MITGLIED");
        
        cs.execute();



Context.xml

  <Resource name="xxxx/xxx/xxxxx" auth="Container"
              type="oracle.jdbc.pool.OracleDataSource"
              driverClassName="oracle.jdbc.OracleDriver"
              factory="oracle.jdbc.pool.OracleDataSourceFactory"
              url="jdbc:oracle:thin:@xx.xx.xxx.xxxx:xxxx:AAAAAA"
              user="xxxxx" password="xxxx" maxTotal="20" maxIdle="10"
              maxWaitMillis="-1"/>   

If anyone has an Idea, id would be great.

Thx

Tagged:
2994778

Best Answer

  • Joe Weinstein-Oracle
    Joe Weinstein-Oracle Member Posts: 516
    edited Jul 20, 2015 10:59AM Accepted Answer

    Hi, yes. You get a pooled connection, which is understandably a wrapper around the naked OracleConnection,

    and you pass it (the still-wrapped connection) to an Oracle method which internally expects to be given a naked

    OracleConnection.

    Try this:

    OracleConnection oconn = connectionDB.unwrap(OracleConnection);

    final StructDescriptor structDescriptor_Mitglied = StructDescriptor.createDescriptor("KPZ.RECTYPE_MITGLIED", oconn); 

    Struct recMitglied = oconn.createStruct("KPZ.RECTYPE_MITGLIED",new Object[]...


    oconn = null; // don't use for standard JDBC, and *don't close it*.

    2994778

Answers

  • Joe Weinstein-Oracle
    Joe Weinstein-Oracle Member Posts: 516
    edited Jul 20, 2015 10:59AM Accepted Answer

    Hi, yes. You get a pooled connection, which is understandably a wrapper around the naked OracleConnection,

    and you pass it (the still-wrapped connection) to an Oracle method which internally expects to be given a naked

    OracleConnection.

    Try this:

    OracleConnection oconn = connectionDB.unwrap(OracleConnection);

    final StructDescriptor structDescriptor_Mitglied = StructDescriptor.createDescriptor("KPZ.RECTYPE_MITGLIED", oconn); 

    Struct recMitglied = oconn.createStruct("KPZ.RECTYPE_MITGLIED",new Object[]...


    oconn = null; // don't use for standard JDBC, and *don't close it*.

    2994778
  • 2994778
    2994778 Member Posts: 3
    edited Jul 21, 2015 2:02AM

    Hi Joe,

    your solution is right.

    Thanks a lot!.

  • Unknown
    edited Jul 21, 2015 11:13AM

    Then please mark the thread ANSWERED.

    And don't forget you need to use proper coding techniques and CHECK that the connection can actually be unwrapped:

    Connection.isWrapperFor()

    Review that method in the Java API - you use it to make sure that the driver code you are using will actually unwrap a connection for you.

    2994778
This discussion has been closed.