Keywords: java | spring | tomcat | datasource | jndi
Abstract: This article provides a comprehensive guide on integrating JNDI DataSource from Tomcat into Spring applications, including XML configurations and code examples for seamless database connectivity management.
In Spring applications, using a JNDI DataSource provided by the container, such as Tomcat, is recommended over simple implementations like DriverManagerDataSource due to better connection pooling and management. This article details the configuration process step by step.
Spring Configuration for JNDI DataSource
To integrate a JNDI DataSource into Spring, you can use XML-based configuration. Spring provides the <jee:jndi-lookup> element for simplified setup, or the JndiObjectFactoryBean for more control.
Example using <jee:jndi-lookup>:
<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.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="dbDataSource"
jndi-name="jdbc/DatabaseName"
expected-type="javax.sql.DataSource" />
</beans>In this code, the jndi-name specifies the JNDI name, and expected-type ensures the object is a DataSource.
Alternatively, using JndiObjectFactoryBean:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>This bean definition looks up the JNDI resource and exposes it as a Spring bean.
Tomcat JNDI Resource Configuration
In Tomcat, you need to define the JNDI resource in the server configuration and link it in the web application context.
First, add a Resource in server.xml under <GlobalNamingResources>:
<GlobalNamingResources>
<Resource name="jdbc/DatabaseName"
auth="Container"
type="javax.sql.DataSource"
username="dbUser"
password="dbPassword"
url="jdbc:mysql://localhost:3306/mydatabase"
driverClassName="com.mysql.cj.jdbc.Driver"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
maxAge="7200000"
testOnBorrow="true"
validationQuery="SELECT 1" />
</GlobalNamingResources>This configuration sets up a DataSource for MySQL with connection pool settings. Note that properties like driverClassName and url should match your database.
Then, in the web application's context.xml, add a ResourceLink:
<Context>
<ResourceLink name="jdbc/DatabaseName"
global="jdbc/DatabaseName"
type="javax.sql.DataSource"/>
</Context>This links the global resource to the web application's JNDI context.
Integration and Usage in Spring
Once configured, the DataSource bean can be injected into other Spring components, such as DAOs or services, using dependency injection.
For example, in a Spring bean:
<bean id="userDao" class="com.example.UserDao">
<property name="dataSource" ref="dbDataSource"/>
</bean>This allows the UserDao to use the JNDI DataSource for database operations.
Conclusion
Using JNDI DataSource in Spring with Tomcat provides a robust solution for database connectivity, leveraging container-managed resources. Always refer to the latest documentation for Tomcat and Spring for any updates in configuration properties.