JDBC needs to operate the database through the connection database connection. The creation of a database connection is expensive, and we cannot create a database connection every time an SQL statement is executed, typically through a database connection pool, but the MySQL database has a 8-hour problem (if the database connection is not used for more than 8 hours, MySQL automatically disconnects the connection), After more than 8 hours, the connection obtained from the connection pool may have been invalidated, resulting in an exception that can be resolved by setting the effective time of the connection in the configuration file of the connection pool, although the connection pool is more beneficial than harmful. At present the more popular connection pool has dbcp, c3p0 and so on. Recently looked under Ali's Open source project Druid, it is not only a database connection pool, also provides the monitoring function, it has the following advantages: 1, very convenient replacement dbcp and C3P0 connection pool, 2, performance than DBCP, C3P0 and other connection pool good, 3, the use of Filter-chain responsibility chain model, It is convenient to add monitoring function and encrypt the database user name and password.
by configuring in Web. xml
<servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.su pport.http.statviewservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> Druidstatview</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
You can count the execution time, number of executions, and so on for the SQL statement. configuring in Web. xml
<filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class> Com.alibaba.druid.support.http.webstatfilter</filter-class> <init-param> <param-name>exclusion S</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </ini t-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <u Rl-pattern>/*</url-pattern> </filter-mapping>
Can count the number of requests per request, request time and other data, Also in the built-in monitoring page weburi-detail.html, view the Jdbcpoolconnectionopencount and Jdbcpoolconnectionclosecount properties, if not equal, indicating a leak in the connection. Some of the specific parameter configurations can refer to Https://github.com/alibaba/druid.
druid-database connection pool with monitoring function