Database connection pool Druid

The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one; release database connections whose idle time exceeds the maximum idle time to avoid failures due to failure to release database connections. Caused by missing database connections. This technique can significantly improve the performance of database operations.
Excerpted from Baidu Encyclopedia

Database connection pool for monitoring

This is its description on GitHub. It has more than 4,000 Stars. These are not important. In fact, his monitoring function is very good. After the development is completed, it provides a lot of references for our code optimization, at least at the database connection level. The improvement is clearly visible. We once optimized a query that took half a minute to the millisecond level. Of course, this is not all about optimizing database queries, but it is very helpful for tuning.

There are many comparisons of commonly used connection pools on the Internet, such as c3p0 , Proxool , Druid , Tomcat Jdbc Pool and so on. You can search for detailed comparisons. I will not discuss them here. I provide a  connection pool c3p0 , Proxool , Druid , Tomcat Jdbc Pool comparison test , you can refer to. Here is the official feature comparison:

You can find them all on GitHub ( https://github.com/alibaba/druid/) .

It is also easy to configure

1. Download and druid-1.0.9.jarimport into the project
. Just download it, address: http://central.maven.org/maven2/com/alibaba/druid/1.0.9/druid-1.0.9.jar

2. applicationContext.xmlFile configuration database connection
This seems to be not much different from other database connection pools, except for a few different parameters, I posted my configuration

<!--master 配置数据源 -->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3306/springdemo?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> <!-- 通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall --> <property name="filters" value="stat,log4j" /> <!-- 最大并发连接数 --> <property name="maxActive" value="30" /> <!-- 初始化连接数量 --> <property name="initialSize" value="5" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="5" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="false" /> <property name="maxOpenPreparedStatements" value="100" /> <!-- 打开removeAbandoned功能(连接泄漏监测,怀疑存在泄漏之后再打开) --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> </bean>

The above parameters can be adjusted according to your needs. For specific configuration, please visit my GitHub example https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/applicationContext.xml

3. web.xmlFile configuration monitoring platform

<!-- 连接池 启用Web监控统计功能   start-->
<filter>
    <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*mp3,*.swf,*.xls,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- 允许清空统计数据 --> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>admin</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>123456</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- 连接池 启用Web监控统计功能 end-->

By adding the above configuration to the file, you can druidaccess the monitoring platform after adding the project address, and check the database connection and SQL statement execution time. Of course, you can configure the login password yourself. After login interface:

in conclusion

Of course, I am here to briefly talk about the basic configuration of the Druid database connection pool. More powerful functions are waiting for you to experience. In our time project, it will be more enjoyable to use than the original c3p0. I would like to thank Xiao Kang in the project team. The technical sharing of Druid, the specific situation and various configurations of Druid can be viewed at  https://github.com/alibaba/druid/  , and the documentation is relatively detailed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447941&siteId=291194637