Tomcat configuration connects to the c3p0 connection pool,
1. Configure the JNDI resource for Tomcat
JNDI (Java Naming and Directory Interface), Java Naming and Directory Interface.
The role of JNDI is to configure resources on the server and then obtain the configured resources in a unified manner.
The resource we want to configure here is of course the connection pool, so that the project can obtain the connection pool object in a unified way.
1. Import package
The Three jar packages need to be placed under the Tomcat/lib/directory: c3p0-0.9.5.2.jar, mchange-commons-java-0.2.11.jar, mysql-connector-java-5.1.44-bin.jar (Driver implementation class), this example is connected to the MySQL database, c3p0-oracle-thin-extras-0.9.5.2.jar is also required if oracle is connected.
2. Configure the context. xml and web. xml files
Add lines 14th to 25 to apache-tomcat-9.0.0.M26/conf/context. xml
1 <Context reloadable = "true"> 2 3 <! -- Default set of monitored resources. If one of these changes, the --> 4 <! -- Web application will be reloaded. --> 5 <WatchedResource> WEB-INF/web. xml </WatchedResource> 6 <WatchedResource> WEB-INF/tomcat-web.xml </WatchedResource> 7 <WatchedResource >$ {catalina. base}/conf/web. xml </WatchedResource> 8 9 <! -- Uncomment this to disable session persistence resume SS Tomcat restarts --> 10 <! -- 11 <Manager pathname = ""/> 12 --> 13 <! -- New configuration content -->
14 <Resource auth = "Container"
15 description = "DB Connection"
16 driverClass = "com. mysql. jdbc. Driver"
17 maxPoolSize = "100"
18 minPoolSize = "2"
19 acquireIncrement = "2"
20 name = "jdbc/mysqlds-c3p0"
21 user = "root"
22 password = ""
23 factory = "org. apache. naming. factory. BeanFactory"
24 type = "com. mchange. v2.c3p0. ComboPooledDataSource"
25 jdbcUrl = "jdbc: mysql: // localhost: 3306/mydb1"/>
26 </Context>
Parameter description:
- Name: Specifies the resource name. This name can be provided as needed when obtaining the resource;
- Factory: The factory used to create resources. This value is basically fixed and does not need to be modified;
- Type: type of the resource. the type we want to provide is the type of the connection pool.
- Other parameters are resource attributes.
Add configuration lines 6th to 10 in the project web/WEB-INF/web. xml file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version = "3.1"> 6 <resource-ref> 7 <res-ref-name> jdbc/mysqlds-c3p0 </res-ref-name> <! -- With context. the name attributes of Resources under xml are consistent --> 8 <res-type> javax. SQL. dataSource </res-type> 9 <res-auth> Container </res-auth> 10 </resource-ref> 11 </web-app>
Ii. obtain resources
1 package servlet; 2 3 import javax. naming. context; 4 import javax. naming. initialContext; 5 import javax. naming. namingException; 6 import javax. servlet. servletException; 7 import javax. servlet. annotation. webServlet; 8 import javax. servlet. http. httpServlet; 9 import javax. servlet. http. httpServletRequest; 10 import javax. servlet. http. httpServletResponse; 11 import javax. SQL. dataSource; 12 import java. io. IOException; 13 import java. SQL. connection; 14 import java. SQL. SQLException; 15 16 @ WebServlet (name = "AServlet", urlPatterns = "/AServlet") 17 public class AServlet extends HttpServlet {18 protected void doGet (HttpServletRequest request, reply response) 19 throws ServletException, IOException {20 // 1. Create the JNDI Context 21 try {22 Context ctx = new InitialContext (); 23 // 2. query entrance 24 // Context envCtx = (Context) ctx. lookup ("java: comp/env"); 25 // 3. Perform a secondary query, find our Resource 26 // The name corresponds to the name of the <Resource> element 27 // DataSource dataSource = (DataSource) envCtx. lookup ("jdbc/mysqlds-c3p0"); 28 DataSource dataSource = (DataSource) ctx. lookup ("java: comp/env/jdbc/mysqlds-c3p0"); 29 Connection con = dataSource. getConnection (); 30 System. out. println (con); 31 con. close (); 32} catch (NamingException e) {33 e. printStackTrace (); 34} catch (SQLException e) {35 e. printStackTrace (); 36} 37 38} 39}