6 Replies Latest reply on Apr 23, 2008 5:32 AM by jaikiran

    ClassCastException accessing a DataSource

    riflevolunteer

      Hi

      I know this as been discussed elsewhere loads of times and I have searched and searched and tried everything and still I get the same error. I'm sure its something simple but I just don't know enough to figure it out.

      I have a MySql database which I am trying to get the DataSource for using JNDI on a JBOSS 4.2.2 see all details below:

      JBoss 4.2.2
      MySQL 5.0
      Eclipse 3.3.2

      JNDI Properties

      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      java.naming.provider.url=jnp://localhost:1099
      java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces


      server/deploy/mysql-ds.xml

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

      <local-tx-datasource>
      <jndi-name>MySqlDS</jndi-name>
      <use-java-context>false</use-java-context>
      <connection-url>jdbc:mysql://localhost:3306/andy_test</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>user</user-name>
      pword
      <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>

      <type-mapping>mySQL</type-mapping>

      </local-tx-datasource>



      simple code

      import java.sql.*;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      public class DataSource {
      public static void main(String[] args) {
      InitialContext ic;
      try {

      InitialContext ic;
      DataSource ds =(DataSource)ic.lookup("MySqlDS");

      } catch (NamingException e) {
      e.printStackTrace();
      }
      catch (ClassCastException e){
      e.printStackTrace();
      }



      }
      }


      When I run this I get this error:

      java.lang.ClassCastException: $Proxy0
      at DataSource.main(DataSource.java:12)

      If anyone can advise me whether I'm doing something fundamentally wrong or a configuration error.

      Thanks



        • 1. Re: ClassCastException accessing a DataSource
          jaikiran

          Please post the entire exception stacktrace. Also, let us know the Java vendor/version that you are using on the client as well as the server. Finally, post the contents of the jndi tree as mentioned at http://wiki.jboss.org/wiki/DisplayTheJDNITreeWithTheJMXConsole

          While posting the logs or code or xml content, remember to wrap it in a code block using the Code button in the message editor window and please hit the Preview button to make sure your post is correctly formatted

          • 2. Re: ClassCastException accessing a DataSource
            riflevolunteer

            Hi

            Thanks for the reply, that is the entire exception stacktrace. The version of java for both the server and client is Sun 1.5.0_15.

            The JNDI tree I cannot find I can find the JMXConsole but nowhere is there anything called jndiview as described in the wiki. Please advise.

            Thanks



            • 3. Re: ClassCastException accessing a DataSource
              jaikiran

               

              "riflevolunteer" wrote:
              Hi


              The JNDI tree I cannot find I can find the JMXConsole but nowhere is there anything called jndiview as described in the wiki. Please advise.



              Did you install JBoss, using the JEMS installer? Just this past week, we had a discussion about this where it turned out that JEMS installer does not configure the JNDIView ( http://www.jboss.com/index.html?module=bb&op=viewtopic&t=133889).

              For JNDIView to be available, you will have to download the JBoss zip archive. Unzip it to some location (which does not contain a space in its folder name Ex: D:\JBoss), set JAVA_HOME and start the server.

              My intention of asking you to post the contents of the jndi tree was to see what the datasource is being bound to. If re-installing JBoss, is not an option for you, can you try out the following piece of code from the client and post the output?

              public static void main(String[] args) {
              
               try {
              
               InitialContext ic;
               Object ds = ic.lookup("MySqlDS");
               Class[] interfaces = ds.getClass().getInterfaces();
               System.out.println("Implemented interfaces: ");
               for (int i = 0; i < interfaces.length; i++) {
               System.out.println("*** " + interfaces);
               }
               }......
               //other stuff
               }


              Let's see if this output gives us some clue.


              • 4. Re: ClassCastException accessing a DataSource
                riflevolunteer

                Hi

                Thanks for your quick reply. Have just run your code and get the following

                Implemented interfaces:
                *** [Ljava.lang.Class;@110b053


                And yes I did run the Jems installer, will reinstall another time for sure.

                Thanks Again

                • 5. Re: ClassCastException accessing a DataSource
                  riflevolunteer

                  Hi Again

                  Have you any idea what could be wrong with my set up.

                  Thanks

                  • 6. Re: ClassCastException accessing a DataSource
                    jaikiran

                     

                    "riflevolunteer" wrote:
                    Hi Again

                    Have you any idea what could be wrong with my set up.

                    Thanks


                    Sorry, i don't exactly know what the problem is, based on what has been posted so far. Can you try looking up the datasource from a servlet and see if that works? Also, is re-installing the JBoss server (using the binary instead of JEMS installer) an option for you? That will atleast show us the JNDIView contents.