6 Replies Latest reply on Apr 23, 2004 3:48 AM by kabirkhan

    Unable to access MySQL datasource

    offline

      Alright, I'm going to admit right off that i am *learning* J2EE programming, and i am currently at a very elementary stage of it. So, if answering questions that seem really obvious to you isn't your bag, i'd stop reading about... now.

      On to the question(s?):

      I'm trying to go through the IBM DeveloperWorks tutorial for EJBs without installing yaeas (Yet Another Enterprise Application Server) on my poor little machine. I currently have JBoss (3.2.??) installed with Tomcat as the web container. I use Eclipse and the MyEclipse plugin to develop.

      I have configured the MySQL datasource and deployed it, with the following XML:

      <datasources>
      <local-tx-datasource>
      <jndi-name>MySQLDS</jndi-name>
      <connection-url>jdbc:mysql://192.168.1.50:3306/webapp</connection-url>
      <driver-class>org.gjt.mm.mysql.Driver</driver-class>
      <user-name>usename</user-name>
      <password>******</password>
      </local-tx-datasource>
      
      </datasources>
      

      And i am trying to access it in a client application like so:
      ic = new InitialContext(env);
      ds = (DataSource)ic.lookup( sJNDIdbName );
      con = ds.getConnection();
      stmt = con.createStatement();
      


      And i get a:

      javax.naming.NameNotFoundException: comp not bound
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:495)
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:503)
      at org.jnp.server.NamingServer.getObject(NamingServer.java:509)
      at org.jnp.server.NamingServer.lookup(NamingServer.java:253)
      (many more...)


      So, much as i'd like to get up and running trying to code examples, i'm stuck trying to figure out what braindead mistakes i've made setting it up. Is there someone here that can help me, or better yet, help me to help myself?

      It's worth noting that this program is not a bean, it's a command line app that i just want to run to insert the tables for a tutorial. It should do nothing but connect to the database. I mention this because i actually have no idea whatsoever *yet) how to do JBoss deployment descriptors, but it's my belief that they're not relevant to this problem. I welcome being disabused, however.

      Thanks in advance for the help.

        • 1. Re: Unable to access MySQL datasource
          poorboy

          It would be worth checking back over the JBoss startup messages to make sure the datasource was bound without error, and also that the JDBC driver is loaded. Other than that, double check that the value of sJNDIdbName is correct.

          • 2. Re: Unable to access MySQL datasource
            kabirkhan

            Also, the Datasource will be bound under java:MySQLDS (or is it java:/MySQLDS, I think they are the same anyway), so you need to look up that if using the global name. Also, objects bound in the java: namespace are not accessible from outside the jvm.

            It lokks like you are
            jvm-a) running JBoss with the datasource defined.
            jvm-b) running a comand-line app trying to get the datasource defined in jvm-a), which is not possible.

            Regards,

            Kab

            • 3. Re: Unable to access MySQL datasource
              offline

              Kab: That sounds quite plausible. I can tell from the JBoss startup messages, and the jmx console, that the data source is bound properly. So, i guess it comes to: How can i best convert this command line app, prior to having a full understanding of J2EE programming, into one that will run in the same JVM and modify the database?

              • 4. Re: Unable to access MySQL datasource
                raja05

                As KabKhan said, java:/ namespace is not available of the JVM in which it is deployed. If you need ur commandline client to work, use java:/ namespace and if u wnt to lookit up from within ur JVM, define a resource-ref in the deployment descriptor(lookup DTD of web.xml or ejb-jar.xml for a how-to) and use the
                java:comp/env/jdbc/RefName

                • 5. Re: Unable to access MySQL datasource
                  offline

                  So just to get this straight: If i use the datasource binding with a command line application i'm wasting my time. There's no way?

                  And also, if i am using the datasource from within a bean, as defined above, i need to use it as java:comp/evn/jdbc/MySQLDS ?

                  • 6. Re: Unable to access MySQL datasource
                    kabirkhan

                     

                    "offline" wrote:
                    So just to get this straight: If i use the datasource binding with a command line application i'm wasting my time. There's no way?

                    correct

                    "offline" wrote:
                    And also, if i am using the datasource from within a bean, as defined above, i need to use it as java:comp/evn/jdbc/MySQLDS ?

                    Within the JVM you can can either
                    -access it via it's global JNDI name: java:/MySQLDS
                    -Bind it in the ENC of your web layer or EJB and access it via something like java:comp/env/jdbc/MySQLDS. To use this you would need to ad the following to your web.ml (if done in web layer) or ejb-jar.xml (if done in ejb):
                    <resource-ref>
                    <res-ref-name>jdbc/MySQLDS</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <res-auth>Container</res-auth>
                    </resource-ref>
                    


                    then add to jboss.xml (if ejb) or jboss-web.xml (if web):
                    <resource-ref>
                    <res-ref-name>jdbc/MySQLDS</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <jndi-name>java:/MySQLDS</jndi-name><!--basically maps comp/env/jdbc/MySQLDS to the global name java:/MySQLDS -->
                    </resource-ref>