6 Replies Latest reply on Nov 6, 2003 5:33 AM by leogsilv

    ClassCastException on DataSource access

    leogsilv

      Hi guys,
      I'm porting my application from jboss 3.2.1 + tomcat to jboss 3.2.2 + tomcat. I got previously configured some Oracle based datasources and all was fine but now , I got ClassCastException when I access a DataSource via JNDI both in jsp and ejb's.
      I did the following code in my jsp test page :

      <%@ page language="java" %>
      <%@ page import="javax.naming.*" %>
      <%@ page import="javax.sql.*" %>
      <%@ page import="java.sql.*" %>
      <%@ page import="org.jboss.resource.adapter.jdbc.*" %>
      <%
      InitialContext context = new InitialContext();
      Object o = context.lookup("java:jdbc/OracleWLogData");
      // prints -> org.jboss.resource.adapter.jdbc.WrapperDataSource@1f7dbd8
      out.println("-> " + o);
      if (o instanceof java.io.Serializable) {
      // this prints
      out.println("is instance of Serializable");
      }
      if (o instanceof javax.sql.DataSource) {
      // this never prints
      out.println("is instance of DataSource");
      }
      //DataSource ds = (DataSource) o;
      // the above line is commented cause it throws ClassCastException
      Class[] interfaces = o.getClass().getInterfaces();
      for(int i = 0 ; i < interfaces.length; i++) {
      // this prints the interfaces : DataSource,Serializable & Referenceable
      out.println("interface : " + interfaces.getName());
      }

      WrapperDataSource ds = (WrapperDataSource)o;
      Connection con = ds.getConnection();
      con.close();
      // the connection opens and close normally
      %>

      Doing the cast to DataSource doesn't works but WrapperDataSource implements DataSource.

      The log of the jca service shows no error. See:
      13:22:54,614 INFO [jdbc/OracleWLogData] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=LocalTxCM,name=jdbc/OracleWLogData to JNDI name 'java:/jdbc/OracleWLogData'
      13:22:54,614 INFO [TxConnectionManager] Started jboss.jca:service=LocalTxCM,name=jdbc/OracleWLogData

      The oracle driver is on the classpath . Please can anyone help me ? Thanks in advance

        • 1. Re: ClassCastException on DataSource access

          What do you see if the ask
          your copy and the WrapperDataSource's copy
          for Class.getClassLoader() on class DataSource

          My guess is you will find they are two different deployments.

          Regards,
          Adrian

          • 2. Re: ClassCastException on DataSource access
            leogsilv

            Hi,
            I added a statement showing the classloader of the interfaces that
            the class WrapperDataSource implements and the result was :
            interface : javax.resource.Referenceable
            classloaders : org.jboss.mx.loading.UnifiedClassLoader3@b8deef{ url=file:/usr/local/jboss-3.2.2/server/default/tmp/deploy/tmp32909jboss-service.xml ,addedOrder=2}
            interface : javax.sql.DataSource
            classloaders : null
            interface : java.io.Serializable
            classloaders : null

            The classloader of WrapperDataSource reference return by the jndi lookup is :
            org.jboss.mx.loading.UnifiedClassLoader3@b8deef{ url=file:/usr/local/jboss-3.2.2/server/default/tmp/deploy/tmp32909jboss-service.xml ,addedOrder=2}

            On JMX Agent View I found the correspondent Classloader:
            UCL=b8deef

            There is nothing special with the oracle-ds.xml and I reinstalled the jboss but the error persists. Any help is welcome

            Thanx

            • 3. Re: ClassCastException on DataSource access

              No.
              I want to see DataSource.class.getClassLoader();
              WrapperDataSource.class.getInterfaces()[indexOfDataSource].getClassLoader();

              Looks like indexOfDataSource is 0 but probably best to print everything in a loop.

              Regards,
              Adrian

              • 4. Re: ClassCastException on DataSource access

                Sorry, where I mention
                WrapperDataSource.class above that should be
                objectReturnedFromJndi.getClass()

                Regards,
                Adrian

                • 5. Re: ClassCastException on DataSource access
                  leogsilv

                  Ok,
                  So, I did a little jsp page :
                  <%@ page language="java" %>
                  <%@ page import="javax.naming.*" %>
                  <%@ page import="javax.sql.*" %>
                  <%@ page import="java.sql.*" %>
                  <%@ page import="org.jboss.resource.adapter.jdbc.*" %>
                  <%
                  InitialContext context = new InitialContext();
                  Object o = context.lookup("java:jdbc/OracleWLogData");
                  WrapperDataSource ds = (WrapperDataSource)o;
                  Class[] interfaces = ds.getClass().getInterfaces();
                  for(int i = 0 ; i < interfaces.length; i++) {
                  out.println("interface : " + interfaces.getName());
                  out.println("classloaders : " + interfaces
                  .getClassLoader());
                  }
                  out.println("WrapperDataSource Classloader : " + ds.getClass().getClassLoader());
                  Connection con = ds.getConnection();
                  Statement st = con.createStatement();
                  ResultSet rs = st.executeQuery("select sysdate from dual");
                  while (rs.next()) {
                  out.println("Return : " + rs.getObject(1));
                  }
                  rs.close();
                  st.close();
                  con.close();
                  %>

                  And the output was :

                  interface : javax.resource.Referenceable
                  classloaders : org.jboss.mx.loading.UnifiedClassLoader3@b8deef{ url=file:/usr/local/jboss-3.2.2/server/default/tmp/deploy/tmp1893jboss-service.xml ,addedOrder=2}
                  interface : javax.sql.DataSource
                  classloaders : null
                  interface : java.io.Serializable
                  classloaders : null
                  WrapperDataSource Classloader : org.jboss.mx.loading.UnifiedClassLoader3@b8deef{ url=file:/usr/local/jboss-3.2.2/server/default/tmp/deploy/tmp1893jboss-service.xml ,addedOrder=2}
                  Return : 2003-11-06 08:33:11.0

                  PS: I'm using the last jdk version (1.4.2) running on a linux machine;

                  Regards
                  Leonardo

                  • 6. Re: ClassCastException on DataSource access
                    leogsilv

                    THE PROBLEM IS RESOLVED !

                    Finally I discovered the problem. The oracle driver jar had the package javax.sql so I pulled out this package , restarted jboss ('cause the driver is under lib) and voilla ! the application can use DataSource normally.Thank you for your help.

                    Regards
                    Leonardo