As a result of the project needs, the use of Tomcat connection pool, from the Internet to check a lot of content, according to the content of the man step, or there is a problem, and finally see the official example to find the correct method.
Website Link: http://tomcat.apache.org/tomcat-8.5-doc/jndi-resources-howto.html
Tomcat version: apache-tomcat-8.5.15
1. Copy the MySQL connection pack Mysql-connector-java-5.1.41-bin.jar file to the Tomcat Lib folder.
2, under the Web application, Meta-inf folder under the new Context.xml, (refer to Tocat context.xml, copy the head of the tail), add
<resource
Name= "Jdbc/t1"
Auth= "Container"
Type= "Javax.sql.DataSource"
maxtotal= "100"
Maxidle= "30"
Maxwaitmillis= "10000"
Username= "Tomcat"
Password= "123456"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:8080:3306/t1"
/>
3, under the WEB application, Web-inf folder under the XML file add:
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/t1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4. Deploy Web Apps to Tomcat, launch Tomcat, no alarms.
5. Applying in code
Context C = new InitialContext ();
DataSource ds = (DataSource) c.lookup ("Java:comp/env/jdbc/t1");
conn = Ds.getconnection ();
The benefits of using connection pooling are:
1, the database operation performance has been improved;
2, through the connection pool management database connection and release, improve the use of system resources efficiency;
Tomcat Database Connection Pool