Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat vs Weblogic JNDI Lookup

Tags:

The Weblogic servers we are using have been configured to allow JNDI datasource names like "appds".

For development (localhost), we might be running Tomcat and when declared in the <context> section of server.xml, Tomcat will hang JNDI datasources on "java:comp/env/jdbc/*" in the JNDI tree.

Problem: in Weblogic, the JNDI lookup is "appds" whilst in Tomcat, it seems that that I must provide the formal "java:comp/env/jdbc/appds". I'm afraid the Tomcat version is an implicit standard but unfortunately, I can't change Weblogic's config ... so that means we end up with two different spring config files (we're using spring 2.5) to facilitate the different environments.

Is there an elegant way to address this. Can I look JNDI names up directly in Tomcat? Can Spring take a name and look in both places? Google searches or suggestions would be great.

like image 432
Luther Baker Avatar asked Sep 06 '08 17:09

Luther Baker


People also ask

Does Tomcat use JNDI?

Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.

What is the difference between Tomcat and WebLogic?

Weblogic is an enterprise and commercial software which requires a license and has a wide variety of features for large-scale industrial applications that eases the life of a developer whereas Tomcat is a lightweight and free open source software which is suitable for small web application or companies where it is cost ...

Which file is used for JNDI lookup in Tomcat?

xml file. This file is located in apache-tomcat/conf directory. The scope of server context. xml file is application, so if you define a DataSource connection pool of 100 connections and there are 20 applications then the datasource will be created for each of the application.

What is the use of JNDI in WebLogic?

The intent of WebLogic JNDI is to provide a naming service for Java EE services, specifically EJB, RMI, and Java Messaging Service (JMS). Therefore, it is important to understand the implications of binding an object to the JNDI tree in a clustered environment.


1 Answers

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> 
like image 55
Leonel Avatar answered Sep 21 '22 12:09

Leonel