Use the Tomcat Jndi data source with spring using the following steps:
1. Copy the database driver under the Lib folder of Tomcat
2. Configure the Tomcat Server.xml configuration file and add a resource node below the Globalnamingresources node as follows:
<GlobalNamingResources> <!--Editable User database that can also is used by Userdatabaserealm to Auth Enticate users--<resource name= "Userdatabase" auth= "Container" type= "Org.apache.catalina.UserD Atabase "description=" User database, can be updated and saved "factory=" Org.apache.catalina. Users. Memoryuserdatabasefactory "pathname=" Conf/tomcat-users.xml "/> <!--|-Name : Represents the name to look for later. With this name you can find DataSource, this name is replaced arbitrarily, but the program is ultimately looking for this name, in order not to be confused with other names, so using Jdbc/oracle, is now configured to a JDBC of the naming of Oracle Services. |-AUTH: Authorized and managed by the container, whether the user name and password can be effective on the container. | Type: This name represents the types that are now Javax.sql.DataSource |-Maxactive: Represents the maximum number of connections a database can open on this server |-Maxidle: Represents the minimum number of connections that a database maintains on this server |-maxwait: Maximum wait Time. 10000 MS |-Username: User name for database connection |-Password: password for database connection |-driverclassname: Database connection Drive Action Program |-URL: Database connection address--<resource name= "Jdbc/mysql" auth= "Container" Type= "Javax.sql.DataSource" maxaction= "maxidle=" "maxwait=" 1000 " Username= "root" password= "db861110" driverclassname= "Com.mysql.jdbc.Driver" Url= "Jdbc:mysql://localhost:3306/restaurant?useunicode=true&characterencoding=utf-8"/> </ Globalnamingresources>
3. Configure the Context.xml file to add a ResourceLink node below the context node, which is consistent with the name attribute of the resource configured in Server.xml.
Global="jdbc/mysql" name="jdbc/mysql" type= " javax.sql.DataSource"/>
4. Configure the Config.properties file within the project as follows:
Datasource.resourceref=truedatasource.jndiname=java:comp/env/jdbc/mysql
5. Introduce a configuration file and create a data source
<!--introduce configuration files--
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property Name= "Locations" > <list> <value>classpath:config.properties</value> </list > </property></bean><bean id= "DataSource" class= " Org.springframework.jndi.JndiObjectFactoryBean "> <property name=" Jndiname "value=" ${ Datasource.jndiname} "></property> <property name=" Resourceref "value=" ${datasource.resourceref} " ></property></bean>
This is configured so that the project can use this Jndi data source.
Spring+tomcat jndi Data source connection pool simple configuration