8 Replies Latest reply on Feb 23, 2011 3:20 AM by sishantkumar

    Class cast exception in context.lookup for datasource.

    sishantkumar

      Hi,

       

      I am trying to look up the JNDI datasource on remote JBoss. My current client is also running on jBoss....

      Here is the client piece of code wher i get the error :

       

      private static DataSource getJndiDataSource(String jndiUrl, String dsName){

              Properties props = new Properties();

              props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

              props.setProperty("java.naming.provider.url", jndiUrl);

              props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

       

              DataSource dataSource=null;

       

              try {

                  System.out.println("inside getJndiDataSource try block");

                  Context ctx = new InitialContext(props);           

                  dataSource = (DataSource)ctx.lookup(dsName);

                  System.out.println("inside getJndiDataSource after lookup" + dataSource.toString());

              } catch (NamingException e) {

                  StringBuffer msg= new StringBuffer(256);

                  msg.append(MessageController.getStackTrace(e));

                  msg.append("\n\nConfig DB Connection is retrieved using JNDI URI "+jndiUrl+" and JNDI DataSource name is "+dsName);

                  MessageController.log(FQCN, 1234, msg.toString(),

                          MessageController.MEDIUM);

              }

       

              return dataSource;

          }

       

      Infact when i try to print the class name from ctx.lookup( )  i am geeting the $Proxy179 as the class name.

      But whilelooking for the datasource (dataSource = (DataSource)ctx.lookup(dsName);) . I get the below error:

       

       

      18:08:36,320 ERROR [STDERR] java.lang.ClassCastException: $Proxy179 cannot be cast to javax.sql.DataSource
      18:08:36,320 ERROR [STDERR]     at com.yodlee.internal.config.db.ConfigKeysCache.getJndiDataSource(ConfigKeysCache.java:709)

       

       

      I have tried adding different jars to my JBOSS lib directory like jbossall-client.jar, jnp-client.jar etc...

       

      If i use a stand alone client to look up the data source, it works fine if i add jbossall-client.jar and others.

       

      Kindly help me ...i have been stuck with this issue for last 4 days :-( ...

       

       

      dataSource = (DataSource)ctx.lookup(dsName);
        • 1. Class cast exception in context.lookup for datasource.
          wolfgangknauf

          Hi,

           

          did you follow this wiki: http://community.jboss.org/wiki/ConfigDataSources (section "Configuring a DataSource for remote usage")?

           

          But see the note in this document:

          "Note: JBoss does not recommend using this feature on a production environment. It requires accessing a connection pool remotely and this is an anti-pattern as connections are not serializable. Besides, transaction propagation is not supported and it could lead to connection leaks if the remote clients are unreliable (i.e crashes, network failure). If you do need to access a datasource remotely, JBoss recommends accessing it via a remote session bean facade."

           

          So, maybe you should use an alternate implementation, if this is for a production application.

           

          Hope this helps

           

          Wolfgang

          • 2. Re: Class cast exception in context.lookup for datasource.
            sishantkumar

            I followed some other link for it.

             

            I am attaching my oracle-ds.xml. I used this to configure data source on jboss server.

             

             

            <?xml version="1.0" encoding="UTF-8"?>

             

            <!-- ===================================================================== -->

            <!--                                                                       -->

            <!--  JBoss Server Configuration                                           -->

            <!--                                                                       -->

            <!-- ===================================================================== -->

             

            <datasources>

              <local-tx-datasource>

                <jndi-name>ConfigDbDS</jndi-name>

                <connection-url>jdbc:oracle:thin:@192.192.192.192:1521:sid</connection-url>

                 <!--

                      See on WIKI page below how to use Oracle's thin JDBC driver to connect with enterprise RAC.

                  -->

                 <!--

                      Here are a couple of the possible OCI configurations.

                      For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

             

                 <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>

                      or

                 <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

             

                      Clearly, its better to have TNS set up properly.

                  -->

                <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>

                <user-name>user</user-name>

                <password>password</password>

             

                <min-pool-size>5</min-pool-size>

                <max-pool-size>100</max-pool-size>

             

             

                <use-java-context>false</use-java-context>

             

                <!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->

                <!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->

                <!-- Checks the Oracle error codes and messages for fatal errors -->

                <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>

                    <!-- sql to call when connection is created

                    <new-connection-sql>some arbitrary sql</new-connection-sql>

                    -->

             

                    <!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered

                    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>

                    -->

             

                  <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->

                  <metadata>

                     <type-mapping>Oracle9i</type-mapping>

                  </metadata>

              </local-tx-datasource>

             

            </datasources>

             


            Regards

            Sishant

            • 3. Class cast exception in context.lookup for datasource.
              sishantkumar

              Hi Knauf,

               

              It seems the datasource is working for remote connection as well as i am using <use-java-context>false</use-java-context>. Moreover my standalone is able to get the datasource form remote machine only.

               

              Regards

              Sishant

              • 4. Class cast exception in context.lookup for datasource.
                wolfgangknauf

                Hi,

                 

                I have to admit that I never used this feature, so I can try to help, but I am no specialist ;-).

                 

                Some things to check:

                -is the Java version the same on server and on client? You don't mix 1.5 and 1.6, I assume?

                -do the JBoss libraries on the client exactly match the ones on the server?

                -does your client also contain the Oracle jars (same version as server)?

                 

                And again: even the JBoss team advices NOT to use this approach ;-)

                 

                Hope one of those helps

                 

                Wolfgang

                • 5. Class cast exception in context.lookup for datasource.
                  jaikiran

                  Sishant Kumar wrote:

                   

                  Moreover my standalone is able to get the datasource form remote machine only.

                   

                  So the lookup is failing for some code that is running in the same JVM as the server? Is the code part of some application that is deployed in JBoss AS? Are you packaging any jar file(s) containing javax.sql.* classes in your application? If yes, try removing those jars from the application packaging.

                  • 6. Class cast exception in context.lookup for datasource.
                    sishantkumar

                    My application is deployed on my local machine in JBoss AS. I am trying to lookup the data source on the JBoss on some other machine.

                    Yes i am packaging the jars.

                     

                    I can search for a particular class file in the jar in for a directory . But i am not able to find a way to search the text within the all the jars in the directory.

                     

                    As of now i am trying to look for the jars for the duplicacy of InitialContext. May be it is because of this duplicacy.

                     

                    Is $Proxy179 a class file in any of the jars within the application?

                    • 7. Re: Class cast exception in context.lookup for datasource.
                      lisbor

                      Hi Sishant,

                      Did you solve the problem?

                      I have pretty much same issue except I'm using XA Datasource for JBoss AS 6.0.0.Final during integration tests (maven surefire). It must be a class loader issue on client side. And yes, a stand alone client looking up the data source works fine.

                      • 8. Re: Class cast exception in context.lookup for datasource.
                        sishantkumar

                        Hi Sergiy,

                         

                        No i could not solve it :-(

                        It hase been put on hold inmy organisation .,..Moreover the Jboss version was haredend according to our needs here.

                        Its becouse of the duplicacy of some jar. You would be required to find the duplcate jar with the class file.

                        Please let me know if you are able to resolve the issue.

                         

                        All d best .

                        Regards