Hibernate with Tomcat JNDI Data source Overview


What is JNDI ?


JNDI stands for Java Naming and Directory Interface

It is a Java API for a directory service that allows Java clients to discover and look up data and objects via a name.

Its just an interface and hence it is independent of the underlying implementation.

In simple words, “It provides a mechanism to bind an object to a name

It can use server, a flat file, or a database based on the configuration.

Why we need to use JNDI ?


JNDI allows distributed applications to look up services in a resource-independent way.

Since its independent of any specific naming or directory service implementation, it enables applications to access different naming and directory services like DNS, LDAP, CORBA and RMI using a common API.

When to use JNDI ?


The most common use case is to set up a database connection pool on a Java EE application server.

Any application that’s deployed on such server(where JNDI is configured) can gain access to the connections they need using the JNDI name without having any knowledge about the connection and the underlying database.
(example : java:comp/env/testPool )

How to configure JNDI in Spring based web application ?


We need to follow below 3 steps to configure JNDI

Step 1

Configure data source in Server and create the JNDI name.

We need to add below entry in tomcat’s server.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
  <Resource name="jdbc/MyTestDB" 
      global="jdbc/MyTestDB" 
      auth="Container" 
      type="javax.sql.DataSource" 
      driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/javainsimpleway" 
      username="root" 
      password="root" 
      
      maxActive="100" 
      maxIdle="20" 
      minIdle="5" 
      maxWait="10000"/>
  <Resource name="jdbc/MyTestDB" 
      global="jdbc/MyTestDB" 
      auth="Container" 
      type="javax.sql.DataSource" 
      driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/javainsimpleway" 
      username="root" 
      password="root" 
      
      maxActive="100" 
      maxIdle="20" 
      minIdle="5" 
      maxWait="10000"/>

Step 2

Update the Resource name in web.xml or context.xml

We need to make the Resource linking either in web.xml or context.xml of Tomcat server as below

web.xml

1
2
3
4
5
6
<resource-ref>
        <description>Mysql  Spring JNDI Datasource</description>
        <res-ref-name>jdbc/MyTestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
        <description>Mysql  Spring JNDI Datasource</description>
        <res-ref-name>jdbc/MyTestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>


Context.xml

1
2
3
4
<ResourceLink name="jdbc/MyTestDB"
              global="jdbc/MyTestDB"
              auth="Container"
              type="javax.sql.DataSource" />
<ResourceLink name="jdbc/MyTestDB"
              global="jdbc/MyTestDB"
              auth="Container"
              type="javax.sql.DataSource" />

Step 3

Configure Spring bean with JNDI Datasource

1
2
3
4
5
6
7
8
9
10
11
<bean id="springDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/MyTestDB"/>
  <property name="lookupOnStartup" value="true"/>
  <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
   <property name="dataSource">
    <ref bean="dataSource" />
    </property>
</bean>
<bean id="springDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/MyTestDB"/>
  <property name="lookupOnStartup" value="true"/>
  <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
   <property name="dataSource">
	<ref bean="dataSource" />
    </property>
</bean>


That’s all, we can inject the sessionFactory in our DAO classes to get the session and access the database using the connection pool managed by the web container.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on