5 Replies Latest reply on Apr 29, 2004 3:28 AM by sesques

    Unable to get a DataSource in a servlet

    Chris Rose Newbie

      I looked through the forum here, and wasn't able to find anything right off, but if this is a frequent one, by all means redirect me.

      Anyway, here goes:

      I want to get access to one of the JCA datasources from a servlet.

      I have defined mysql-ds.xml as follows:

      <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>x</user-name>
       <password>y</password>
       </local-tx-datasource>
      
      </datasources>
      


      My web.xml has the following:
       <resource-ref>
       <description>The default DS</description>
       <res-ref-name>java:/MySQLDS</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
      


      and jboss-web.xml has
      <resource-ref>
       <res-ref-name>jdbc/DefautDS</res-ref-name>
       <jndi-name>java:/comp/env/MySQLDS</jndi-name>
      </resource-ref>
      


      I tried the following strings in the resource-ref definition in web.xml:

      java:/MySQLDS (to try the direct jndi name)
      java:/comp/env/MySQLDS (because i hear that comp/env is used somewhere?)
      jdbc/DefaultDS (and every other possible string that i could think of, which i also simulatneously changed in jboss-web.xml)
      java:jdbc:/MySQLDS

      Can someone tell me what i have to do to get rid of this exception?

      [MainDeployer] could not start deployment: file:/D:/dev/jboss-3.2.3/server/default/deploy/sqlApp.war/
      org.jboss.deployment.DeploymentException: Error during deploy; - nested throwable: (javax.naming.NamingException: resource-ref: java:/MySQLDS has no valid JNDI binding. Check the jboss-web/resource-ref.)


        • 1. Re: Unable to get a DataSource in a servlet
          sesques Novice

          Try:

          For mysql-ds.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>x</user-name>
           <password>y</password>
           </local-tx-datasource>
          </datasources>
          


          for web.xml :
           <resource-ref>
           <description>The default DS</description>
           <res-ref-name>MySQLDS</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
           </resource-ref>
          


          for jboss-web.xml :
          <resource-ref>
           <res-ref-name>MySQLDS</res-ref-name>
           <jndi-name>java:/MySQLDS</jndi-name>
          </resource-ref>
          


          and code:
          Context initialContext = new InitialContext();
          Context env = (Context) initialContext.lookup("java:comp/env");
          DataSource ds = (DataSource) env.lookup("MySQLDS");
          


          I Hope it helps


          • 2. Re: Unable to get a DataSource in a servlet
            Chris Rose Newbie

            It does, although i'd like to know why -- what was it that i was doing wrong in my naming?

            In a slightly related note, where should i be putting the driver jar file for this? I'm getting a ClassNotFoundException when i try to initialize the connection, so obviously i'm going about it wrong. My mysql driver jar file is currently in $JBOSS_HOME/lib

            • 3. Re: Unable to get a DataSource in a servlet
              rupshall Newbie

              I have my jdbc jar file in $JBOSS_HOME/servers/default/lib, it needs to be in the lib under whatever server you are using.

              • 4. Re: Unable to get a DataSource in a servlet
                Chris Rose Newbie

                Thanks :)

                I figured that one out with embarassingly little googling. Silly me.

                At any rate, i still don't know what i was doing wrong in web.xml and jboss-web.xml. I'd love to hear, though.

                • 5. Re: Unable to get a DataSource in a servlet
                  sesques Novice

                  1) The <res-ref-name> in web.xml and jboss-web.xml must be the same.
                  2) According to your mysql-ds.xml, the jndi name for your datasource is java:/MySQLDS at deployment time because the comp/env environment is not yet set.
                  3) Then, in your servlet, the lookup mus be done in java:comp/env/<res-ref-name>

                  Is this what you want ot know ?