Using the Listener step
1. Defining the Listener implementation class
2. Configuring (or using annotation) in Web. xml
Create a jar package to add a database connection pool using the C3p0 method
1.c3p0-0.9.5.jar
2.c3p0-oracle-thin-extras-0.9.5.jar
3.mchange-commons-java-0.2.9.jar
1 package cn.sdut.lah.listener;
Two
3 import java.sql.Connection;
4 import javax.servlet.ServletContext;
5 import javax.servlet.ServletContextEvent;
6 import javax.servlet.ServletContextListener;
Seven
8 import com.mchange.v2.c3p0.ComboPooledDataSource;
Nine
Ten
11 public class GetConnListener implements ServletContextListener {
Twelve
13 / / when starting the web application, this method is called
14 @Override
15 public void contextInitialized(ServletContextEvent sce) {
16 try {
17 / / obtain the ServletContext instance of the application
18 ServletContext application = sce.getServletContext();
Nineteen
20 / / get the driver from the configuration parameter
21 String driver = application.getInitParameter("driver");
22 / / get the database URL from the configuration parameter
23 String url = application.getInitParameter("url");
24 / / get the user name from the configuration parameter
25 String user = application.getInitParameter("user");
26 / / obtain the password from the configuration parameter
27 String password = application.getInitParameter("pass");
Twenty-eight
29 / / create connection pool instance
30 ComboPooledDataSource cpds = new ComboPooledDataSource();
31 / / set the drivers required by the connection pool to connect to the database
32 cpds.setDriverClass(driver);
33 / / set the driver required to connect to the database
34 cpds.setJdbcUrl(url);
35 / / set the user name to connect to the database
36 cpds.setUser(user);
37 / / set the password to connect to the database
38 cpds.setPassword(password);
39 / / set the maximum number of connections in the connection pool
40 cpds.setMaxPoolSize(40);
41 / / set the minimum number of connections in the connection pool
42 cpds.setMinPoolSize(2);
43 / / set the initial number of connections in the connection pool
44 cpds.setInitialPoolSize(10);
45 / / set the maximum number of cached statements for the connection pool
46 cpds.setMaxStatements(180);
Forty-seven
48 / / set datasource to application scoped property
49 application.setAttribute("ds", cpds);
Fifty
51 } catch (Exception e) {
52 e.printStackTrace();
53}
Fifty-four
55}
Fifty-six
57 / / close web application
58 @Override
59 public void contextDestroyed(ServletContextEvent sce) {
60 ServletContext application = sce.getServletContext();
61 ComboPooledDataSource ds = (ComboPooledDataSource) application
62 .getAttribute("ds");
63 try {
64 Connection conn = ds.getConnection();
65 if (conn != null) {
66 conn.close();
67 }
68 } catch (Exception e) {
69 e.printStackTrace();
70}
Seventy-one
72}
Seventy-three
74}
Configuring parameters in Web. xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.0"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
6
7 <context-param>
8 <param-name>driver</param-name>
9 <param-value>com.mysql.jdbc.Driver</param-value>
10 </context-param>
11
12 <context-param>
13 <param-name>url</param-name>
14 <param-value>jdbc:mysql://localhost:3306/javaee</param-value>
15 </context-param>
16
17 <context-param>
18 <param-name>user</param-name>
19 <param-value>root</param-value>
20 </context-param>
21
22 <context-param>
23 <param-name>pass</param-name>
24 <param-value>ab123456</param-value>
25 </context-param>
36
37 <listener>
38 <listener-class>cn.sdut.lah.listener.GetConnListener</listener-class>
39 </listener>
40
41 </web-app>
Create a database connection pool by implementing the Servletcontextlistener interface (C3P0 mode)