JNDI configuration of Spring data source compatible with Tomcat and Weblogic

Posted by biscutty on Wed, 13 Nov 2019 18:54:52 +0100

As we all know, the name of looking up JNDI in Tomcat and Weblogic is different. Take Spring configuration for example. In Weblogic, you need to configure as follows:

Java code

<bean id="baseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
      <property name="jndiName" value="dataSource" />  
</bean> 

 

But in Tomcat it's like this:

Java code

<bean id="baseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
      <property name="jndiName" value="java:comp/env/dataSource" />  
</bean>   

When Tomcat is used for development (fast speed) and Weblogic is deployed again, how to be compatible is a headache. Of course, we can configure the way of JDBC direct connection:

Java code

<bean id="baseDataSource"  
       class="org.apache.commons.dbcp.BasicDataSource"  
       destroy-method="close">  
       <property name="driverClassName" value="oracle.jdbc.OracleDriver" />  
       <property name="url" value="jdbc:oracle:thin:@192.168.1.239:1521:dev1" />  
       <property name="username" value="foo" />  
       <property name="password" value="bar" />  
       <property name="initialSize" value="2" />  
       <property name="maxActive" value="15" />  
       <property name="testWhileIdle" value="true" />  
       <property name="validationQuery" value="select 1 from dual" />  
       <property name="testOnBorrow" value="true" />  
</bean>  

In this way, it can be deployed in Tomcat and Weblogic, but the disadvantages are also obvious. If you want to modify the database connection information, you have to change the Spring configuration!  

We can use the JNDI lookup content pattern introduced by spring 2.0 to improve the configuration (note that the xsd of jee is introduced at the beginning of the file):

Java code

<?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-2.0.xsd    
                http://www.springframework.org/schema/jee     
                http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">    
   
    <jee:jndi-lookup id="baseDataSource" jndi-name="dataSource" resource-ref="true"/>  

Note the resource ref = "true" here. When the resource ref attribute is true, jndiName will be added with java:comp/env /. This is where Tomcat finds JNDI, so Tomcat can use it!  

As for Weblogic, it is possible to find JNDI directly through JNDI name attribute, so it can also be used.  

 

Attached is the JNDI configuration of Tomcat7, which is global, so it is configured in% Tomcat? Home% \ conf \ context.xml:

Java code

     

<Resource name="dataSource" auth="Container"  
           type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"  
           url="jdbc:oracle:thin:@192.168.1.239:1521:dev1"  
           username="foo" password="bar" maxActive="15" maxIdle="10" initialSize="2"  
           testWhileIdle="true" validationQuery="select 1 from dual" testOnBorrow="true"/>  

I use oracle to copy the driver to% Tomcat? Home% \ lib. web.xml does not need to be configured.

Reference address:

http://blog.csdn.net/yangfanend/article/details/7516192

Topics: Tomcat Java Weblogic Spring