Tomcat vs Weblogic JNDI Lookup

How to use a single JNDI name in your web app

I’ve struggled with this for a few months myself. The best solution is to make your application portable so you have the same JNDI name in both Tomcat and Weblogic.

In order to do that, you change your web.xml and spring-beans.xml to point to a single jndi name, and provide a mapping to each vendor specific jndi name.

I’ve placed each file below.

You need:

  • A <resource-ref /> entry in web.xml for your app to use a single name
  • A file WEB-INF/weblogic.xml to map your jndi name to the resource managed by WebLogic
  • A file META-INF/context.xml to map your jndi name to the resource managed by Tomcat
    • This can be either in the Tomcat installation or in your app.

As a general rule, prefer to have your jndi names in your app like jdbc/MyDataSource and jms/ConnFactory and avoid prefixing them with java:comp/env/.

Also, data sources and connection factories are best managed by the container and used with JNDI. It’s a common mistake to instantiate database connection pools in your application.

spring

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<jee:jndi-lookup jndi-name="jdbc/appds"
                 id="dataSource" />
</beans>

web.xml

<resource-ref>
    <description>My data source</description>
    <res-ref-name>jdbc/appds</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

weblogic.xml

<?xml version="1.0" encoding="UTF-8" ?>
<weblogic-web-app
    xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.oracle.com/weblogic/weblogic-web-app http://http://www.oracle.com/technology/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd">

<resource-description>
    <jndi-name>appds</jndi-name>
    <res-ref-name>jdbc/appds</res-ref-name>
</resource-description>
</weblogic-web-app>

META-INF/context.xml (for Tomcat)

<Context>
    <ResourceLink global="jdbc/appds" name="jdbc/appds" type="javax.sql.DataSource"/>
</Context>

Leave a Comment