4 Replies Latest reply on Sep 10, 2008 1:32 PM by peterj

    ClassCastException when calling DatabaseMetadatat functions

    tonioinho

      Hi,

      Using JbossAS 4.2.2GA and Oracle 10g, I access a DataSource through JNDI lookup.

      Everything works fine until I try to call methods of DatabaseMetadata which return a ResultSet.


       InitialContext ic = new InitialContext();
       DataSource datasource = (DataSource) ic.lookup("Test/DataSource/Data");
      
       Connection conn = null ;
      
       Statement stmt = null;
       ResultSet rs = null;
       try
       {
       conn = datasource.getConnection();
      
       stmt = conn.createStatement();
       rs = stmt.executeQuery(
       "select DISTINCT " +
       "a.ID, " +
       "a.NAME " +
       "from TEST_TABLE a" );
      
       for( int nbRecords= 1; rs.next(); ++nbRecords ) {
      
       String id = rs.getString( 1 );
       String name = rs.getString( 2 );
      
       System.out.println(String.format("%1$s: %2$s", id, name));
      
       }
      
       DatabaseMetaData metaData = conn.getMetaData();
       String catalogTerm = metaData.getCatalogTerm();
       System.out.println("CatalogTerm :"+catalogTerm);
       //output is as expected
      
       ResultSet resultset = metaData.getColumns(conn.getCatalog(), null, "TEST_TABLE", "NAME");// throws an exception
      


      The exception :
       java.lang.ClassCastException: $Proxy5 cannot be cast to java.sql.ResultSet
       at $Proxy4.getColumns(Unknown Source)
      



        • 1. Re: ClassCastException when calling DatabaseMetadatat functi
          tonioinho

          The related datasource file :

          <?xml version="1.0" encoding="UTF-8"?>
          
          <datasources>
          
           <local-tx-datasource>
          
           <!-- Setting this to false will bind the DataSource into global jndi -->
           <use-java-context>false</use-java-context>
          
           <jndi-name>Test/DataSource/Data</jndi-name>
          
           <connection-url>jdbc:oracle:thin:@localhost:1521:XE</connection-url>
           <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
           <user-name>username</user-name>
           <password>password</password>
           <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
           <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
           <min-pool-size>2</min-pool-size>
           <max-pool-size>20</max-pool-size>
           <blocking-timeout-millis>5000</blocking-timeout-millis>
           <idle-timeout-minutes>10</idle-timeout-minutes>
           <prefill>true</prefill>
           <metadata>
           <type-mapping>Oracle9i</type-mapping>
           </metadata>
          
           </local-tx-datasource>
          
          </datasources>
          


          Also the ojdbc14.jar file is in the server/default/lib folder

          The code works fine using Mysql or SQL Server datasources.

          I found a similar post ( http://www.jboss.com/index.html?module=bb&op=viewtopic&t=87423 ) but it is not answered.

          thanks in advance





          • 2. Re: ClassCastException when calling DatabaseMetadatat functi
            peterj

            Please post more of the stack trace - the first line is not sufficient. From what you posted I cannot tell if the issue is with the JDBC driver (which is what I would guess) or with some code in JBoss (not likely because as you said "The code works fine using Mysql or SQL Server datasources").

            Also, what JDBC driver are you using for Oracle?

            • 3. Re: ClassCastException when calling DatabaseMetadatat functi
              tonioinho

              Thanks for the quick answer.

              This is the full stack trace I get (except the line referring to the calling code).

              I should have mentioned that the code is called from a remote client and that the metadata.getColumns() function works fine inside a session bean.

              The driver is the one provided by Oracle : Oracle JDBC driver 10.2.0.1.0

              Regards,

              • 4. Re: ClassCastException when calling DatabaseMetadatat functi
                peterj

                There are issues with accessing a data source from a remote client. Looks like you just ran into one of them.

                I would guess that there is some issue with serializing an object that the JDBC driver created. Try adding the JDBC driver JAR file to your client's classpath. If that doesn't work, you will have to debug the problem to determine the actual cause and then decide how to fix it.