• Now all data be ready for publish, We had made our blog alive now. Haha.

  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…

How to use JNDI DataSource provided by Tomcat in Spring?- Stack Overflow

Java PingBook 2 months ago (10-23) 18 0


It is said that in the Spring javadoc article about DriverManagerDataSource class, that this class is very simple and that it is recommended

to use a JNDI DataSource provided by the container. Such a DataSource can be exposed as a DataSource bean in a Spring ApplicationContext via JndiObjectFactoryBean

The question is: how do I accomplish this?

For example, if I wish to have DataSource bean to access my custom MySQL database, what would I require then? What should I write in the context configuration, etc?


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

Alternatively, setup using simple bean configuration like this:

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

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

Reference documentation:

Tomcat 8 JNDI Datasource HOW-TO
Tomcat 8 Context Resource Links Reference
Spring 4 JEE JNDI Lookup XML Schema Reference
Spring 4 JndiObjectFactoryBean Javadoc

Edit: This answer has been updated for Tomcat 8 and Spring 4. There have been a few property name changes for Tomcat’s default datasource resource pool setup.

Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:How to use JNDI DataSource provided by Tomcat in Spring?- Stack Overflow
LIKE (0)
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website