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
DataSourcecan be exposed as aDataSourcebean in a Spring ApplicationContext viaJndiObjectFactoryBean
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:
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.