Druid connection pool

  • Introduction to Druid Connection Pooling

Druid is first and foremost a database connection pool. Druid is currently the best database connection pool. In terms of function, performance and scalability, it surpasses other database connection pools, including DBCP, C3P0, BoneCP, Proxool, and JBoss DataSource. Druid has deployed more than 600 applications in Alibaba, and has undergone a rigorous test of large-scale deployment in production environments for more than a year.

At the same time, Druid is not just a database connection pool, it includes four parts:

    Druid is a JDBC component that consists of three parts:

    Plug-in system based on Filter-Chain mode.

    DruidDataSource Efficient and manageable database connection pool.

    SQLParser

Features of Druid

1. Replace DBCP and C3P0. Druid provides an efficient, powerful, and scalable database connection pool.

2. It can monitor database access performance. Druid provides a powerful StatFilter plug-in built-in, which can calculate the execution performance of SQL in detail, which is helpful for online analysis of database access performance.

3. Database password encryption. It is bad behavior to directly write the database password in the configuration file, which can easily lead to security problems. Both DruidDruiver and DruidDataSource support PasswordCallback.

4. SQL execution log, Druid provides different LogFilters, which can support Common-Logging, Log4j and JdkLog. You can select the corresponding LogFilter as needed to monitor the database access of your application.

5. Extend JDBC. If you have programming requirements for the JDBC layer, you can easily write extension plug-ins for the JDBC layer through the Filter mechanism provided by Druid.

  • Reason for use

             The original project used the C3P0 connection pool. After the project was released and used for a period of time, it was found that when the c3p0 connection pool accesses the database, the connection created will be recorded in the lisenter.log log file of oralce. 

After a period of observation, it is found that oralce will log in the lisenter.log every 6 seconds. The minimum connection we set is 20, so oracle will log 20 records each time. As time goes on, the log file gets bigger and bigger, and when the log file reaches 4 G, it will cause oracle to die. After continuously adjusting the parameter configuration, I still could not get a solution. Finally, I chose to use the Alibaba Druid connection pool to try. Druid defaults to a minimum of 2 connections. After the configuration is completed, the observation log is released and it is found that only 2 records are recorded in the log when it is created. Not logging every 6 seconds like C3P0 does cause the log file to get bigger and bigger. 

lisenter.log log screenshot

 

  • Druid use

       1. Druid.jar包

                       druid-1.1.4.jar   1.1.4

       2. Druid connection pool configuration

                     

           There is no minimum number of connections configured here, the default is 2.

        3. Druid property configuration diagram

configure Default value
illustrate
name   The significance of configuring this property is that if there are multiple data sources, they 
can be distinguished by name when monitoring. If not configured, a name will be generated in the 
format: "DataSource-" + System.identityHashCode(this)
jdbcUrl   The url to connect to the database is different for different databases. For example: 
mysql : jdbc:mysql://10.20.153.104:3306/druid2  
oracle : jdbc:oracle:thin:@10.20.149.85:1521:ocnauto
username   Username to connect to the database
password   连接数据库的密码。如果你不希望密码直接写在配置文件中, 
可以使用ConfigFilter。详细看这里: 
https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
driverClassName 根据url自动识别 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName
initialSize 0 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
maxActive 8 最大连接池数量
maxIdle 8 已经不再使用,配置了也没效果
minIdle   最小连接池数量
maxWait   获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
poolPreparedStatements false 是否缓存preparedStatement,也就是PSCache。 
PSCache对支持游标的数据库性能提升巨大,比如说oracle。 
在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。
作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 
该应该是支持PSCache。
maxOpenPreparedStatements -1 要启用PSCache,必须配置大于0,当大于0时, 
poolPreparedStatements自动触发修改为true。 
在Druid中,不会存在Oracle下PSCache占用内存过多的问题, 
可以把这个数值配置大一些,比如说100
validationQuery   用来检测连接是否有效的sql,要求是一个查询语句。 
如果validationQuery为null,testOnBorrow、testOnReturn、 
testWhileIdle都不会其作用。
testOnBorrow true 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnReturn false 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testWhileIdle false 建议配置为true,不影响性能,并且保证安全性。 
申请连接的时候检测,如果空闲时间大于 
timeBetweenEvictionRunsMillis, 
执行validationQuery检测连接是否有效。
timeBetweenEvictionRunsMillis   有两个含义: 
1) Destroy线程会检测连接的间隔时间 
2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
numTestsPerEvictionRun   不再使用,一个DruidDataSource只支持一个EvictionRun
minEvictableIdleTimeMillis    
connectionInitSqls   物理连接初始化的时候执行的sql
exceptionSorter 根据dbType自动识别 当数据库抛出一些不可恢复的异常时,抛弃连接
filters   属性类型是字符串,通过别名的方式配置扩展插件, 
常用的插件有: 
监控统计用的filter:stat  
日志用的filter:log4j 
防御sql注入的filter:wall
proxyFilters   类型是List<com.alibaba.druid.filter.Filter>, 
如果同时配置了filters和proxyFilters, 
是组合关系,并非替换关系

   

    4.数据源监控配置

          在项目的web.xml加上以下代码

                              

复制代码
 1 <filter>
 2         <filter-name>DruidWebStatFilter</filter-name>
 3         <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
 4         <init-param>
 5             <param-name>exclusions</param-name>
 6             <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
 7         </init-param>
 8     </filter>
 9     <filter-mapping>
10         <filter-name>DruidWebStatFilter</filter-name>
11         <url-pattern>/*</url-pattern>
12     </filter-mapping>
13     
14     <servlet>
15         <servlet-name>DruidStatView</servlet-name>
16         <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
17     </servlet>
18     <servlet-mapping>
19         <servlet-name>DruidStatView</servlet-name>
20         <url-pattern>/druid/*</url-pattern>
21     </servlet-mapping>
复制代码

     阿里推出的这个数据源监控很强大,有喜欢的同志请自己配置玩玩这里我就不做解释了。

     下面贴出监控数据源截图

           

原文地址:https://www.cnblogs.com/txsblog/p/7692458.html

Guess you like

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