1 Reply Latest reply on Oct 5, 2009 9:11 AM by Peter Johnson

    What is the exact correct way to make JBoss datasource JNDI

    Matt Jiang Newbie

      Hi All,

      I saw many people got confused of configuring DataSource JNDI on JBoss, I saw two major example, but for me only one works.

      I use Spring 2.5.6, Jboss 4.2.2GA, Sun JDK 1.5

      Example 1:(works)

      mysql-ds.xml

      <?xml version="1.0"?>
      <datasources>
       <local-tx-datasource>
       <jndi-name>jdbc/esoafds</jndi-name>
       <connection-url>jdbc:mysql://192.168.1.2:3306/jbranch?useUnicode=true&characterEncoding=UTF8&autoReconnect=true</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>root</user-name>
       <password></password>
       <min-pool-size>3</min-pool-size>
       <max-pool-size>100</max-pool-size>
       <use-java-context>false</use-java-context>
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources>


      Spring applicationContext.xml
      <jee:jndi-lookup id="dataSource" jndi-name="jdbc/esoafds" resource-ref="false" />


      Above example, could someone advises me why I have to add
      <use-java-context>false</use-java-context>
      to make this config works?

      Example 2:(It should work, but NOT)
      mysql-ds.xml

      <?xml version="1.0"?>
      <datasources>
       <local-tx-datasource>
       <jndi-name>jdbc/esoafds</jndi-name>
       <connection-url>jdbc:mysql://192.168.1.2:3306/jbranch?useUnicode=true&characterEncoding=UTF8&autoReconnect=true</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>root</user-name>
       <password></password>
       <min-pool-size>3</min-pool-size>
       <max-pool-size>100</max-pool-size>
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources>

      Please be aware that no "use-java-context" tag needed.

      web.xml
      <resource-ref>
       <description>Esoaf DataSource</description>
       <res-ref-name>jdbc/esoafds</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>


      jboss-web.xml
      <jboss-web>
      
       <resource-ref>
       <res-ref-name>jdbc/esoafds</res-ref-name>
       <jndi-name>java:jdbc/esoafds</jndi-name>
       </resource-ref>
      
      </jboss-web>


      Spring applicationContext.xml
      <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="java:jdbc/esoafds"/>
       </bean>


      Should I add "java:" before "jdbc/esoafds" or not?

      The second example can find correct JNDI name from JBoss, but it returns null from JNDI look up.
      Can any body give me a hint what's the concept difference between the two approaches? Why my example 1 works, but exmple 2 return null look up.

      Thanks a lot.

        • 1. Re: What is the exact correct way to make JBoss datasource J
          Peter Johnson Master

           

          Above example, could someone advises me why I have to add
          Code:

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

          to make this config works?

          The use-java-context setting determines where in the jndi tree the name appears. If this is true (the default), the the full name is "java:/jdbc/esoafds". If the value is false then the full name is "jdbc/esoafds".

          Only apps deployed into the application server can see names in the "java:" context, so if you are using a remote client to access a name, it cannot appear there. Thus you need use-java-context set to false.

          In you second example, the web.xml, in combination with the jboss-web.xml, is saying that any time the app requests the name "jdbc/esoafds" to actually look at "java:jdbc/esoafds". But your ap is a Spring app and you are telling it the wrong name - try "jdbc/esoafds" instead.

          Disclaimer: its been years since I used Spring and I don't know what funny things it does with JNDI names when looking things up, so you might have to play around with the names for a while. Also, you should use jNDIView to see what names you really have.