4 Replies Latest reply on Jul 23, 2009 1:29 PM by Peter Johnson

    Using jndi from a web project

    Abu Badila Newbie

      I have a project which I want to deploy. It uses JSF for creating the interface and spring for bean handling. I can deploy it to tomcat 6.0.20 without problems, but I have JNDI issues when I try to do so in jboss 4.2.3.GA.

      I use jndi to connect to the mysql database. If I set

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

      in jboss-ds.xml then I can connect to it via
      new InitialContext().lookup("myname");


      Otherwise, if I set use-java-context to true, I cannot connect using:
      new InitialContext().lookup("java:myname");

      or using anything else, for that matter.

      The error I get is:
      PoolJDBCServiceLocator, method getConnection: Error resolving service: javax.naming.NameNotFoundException: jdbc not bound

      Although, during the startup jboss announces:
      INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=pCollecta' to JNDI name 'java:myname'


      I'm not a JNDI expert, but reading jboss documentation regarding JNDI didn't get me any new insight. Maybe you can help me out or point me to some documentation that may teach me how to do this properly.

      These are the other field in jboss-ds.xml which I have set:
      <local-tx-datasource>
       <jndi-name>myname</jndi-name>
       <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>x</user-name>
       <password>y</password>
       <min-pool-size>5</min-pool-size>
       <max-pool-size>20</max-pool-size>
       <idle-timeout-minutes>5</idle-timeout-minutes>
      </local-tx-datasource>


        • 1. Re: Using jndi from a web project
          Abu Badila Newbie

          I'm sorry, the error message is actually:

          java.lang.Exception: Datasource is not builded. JNDI pool name is invalid or application server JNDI database connection pool service is invalid. Verify your database settings.


          But all the rest still stands.


          • 2. Re: Using jndi from a web project
            Peter Johnson Master

            If you set use-java-context to true (the default), then the datasource name is actually "java:myname" based on your example (you can check with JNDIView to verify this).

            However, you cannot connect to the java:/nyname name from a remote client - only apps deployed within JBoss AS can access things in the "java:" namespace.

            I know that from a web app you need to provide a few configuration settings to get the connection made. First, you need to use this name:

            java:comp/env/myname

            then you need this entry in web.xml:

            <resource-ref>
             <res-ref-name>myname</res-ref-name>
             <res-type>javax.sql.DataSource</res-type>
             <res-auth>Container</res-auth>
             <res-sharing-scope>Shareable</res-sharing-scope>
             </resource-ref>


            and this entry in WEB-INF/jboss-web.xml:

            <jboss-web>
             <resource-ref>
             <res-ref-name>myname</res-ref-name>
             <jndi-name>java:myname</jndi-name>
             </resource-ref>
            </jboss-web>


            • 3. Re: Using jndi from a web project
              Abu Badila Newbie

              I have tried that already, but it still doesn't work. I can't figure out why. When I first read about it (http://forums.sun.com/thread.jspa?threadID=626778) it looked like "java:myname" had to work, even without using the reference thing. So I thought the root of my problem was that I couldn't access it in the first place.

              I'm out of ideas, I think I'll just make a conditional in the code so it will work on jboss and tomcat differently.

              • 4. Re: Using jndi from a web project
                Peter Johnson Master

                Saving possible typos that I made, what I posted is what I use for one of my apps, only the datasource name was changed. Of course, that's not to say it is the only way.