How to use JNDI DataSource provided by Tomcat in Spring?

Answer

If using Spring's XML schema based configuration, setup in the Spring context like this:

<xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee 
                    http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">...<jee:jndi-lookup id="dbDataSource"
   jndi-name="jdbc/DatabaseName"
   expected-type="javax.sql.DataSource"/>

Alternatively, setup using simple bean configuration like this:

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

You can declare the JNDI resource in tomcat's server.xml using something like this:

<GlobalNamingResources><Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
              username="dbUsername" password="dbPasswd"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="5" maxWait="5000"
              maxActive="120" maxIdle="5"
              validationQuery="select 1"
              poolPreparedStatements="true"/></GlobalNamingResources>

And reference the JNDI resource from Tomcat's web context.xml like this:

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

All tomcat Questions

Ask your interview questions on tomcat

Write Your comment or Questions if you want the answers on tomcat from tomcat Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---