c3p0,DBPC,Druid三大连接池的区别/性能面试+工作
Posted Java帮帮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c3p0,DBPC,Druid三大连接池的区别/性能面试+工作相关的知识,希望对你有一定的参考价值。
先来点实用的:
<!-- 配置dbcp数据源 -->
<bean id="dataSource2" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 池启动时创建的连接数量 -->
<property name="initialSize" value="5"/>
<!-- 同一时间可以从池分配的最多连接数量。设置为0时表示无限制。 -->
<property name="maxActive" value="30"/>
<!-- 池里不会被释放的最多空闲连接数量。设置为0时表示无限制。 -->
<property name="maxIdle" value="20"/>
<!-- 在不新建连接的条件下,池中保持空闲的最少连接数。 -->
<property name="minIdle" value="3"/>
<!-- 设置自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!-- 自动回收超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="200"/>
<!-- 设置在自动回收超时连接的时候打印连接的超时错误 -->
<property name="logAbandoned" value="true"/>
<!-- 等待超时以毫秒为单位,在抛出异常之前,池等待连接被回收的最长时间(当没有可用连接时)。设置为-1表示无限等待。 -->
<property name="maxWait" value="100"/>
</bean>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="100" />
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="30" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="0" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="30" />
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
<property name="breakAfterAcquireFailure" value="true" />
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout" value="false" />
</bean>
<!-- 阿里 druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${db.mysql.url}" />
<property name="username" value="${db.mysql.username}" />
<property name="password" value="${db.mysql.password}" />
<property name="driverClassName" value="${driverClassName}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="10" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<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" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 连接泄漏处理。Druid提供了RemoveAbandanded相关配置,用来关闭长时间不使用的连接(例如忘记关闭连接)。 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 配置监控统计拦截的filters, 监控统计:"stat",防SQL注入:"wall",组合使用: "stat,wall" -->
<property name="filters" value="stat" />
</bean>
区别比较
c3p0、dbcp、druid三大连接池的区别 |
|||||
c3p0 | c3p0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSources 对象。 |
简 介 |
|||
DBCP | DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序中使用,Tomcat的数据源使用的就是DBCP。 | ||||
druid | 阿里出品,淘宝和支付宝专用数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个 SQL Parser。支持所有JDBC兼容的数据库,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。Druid针对Oracle和MySql做了特别优化,比如Oracle的PS Cache内存占用优化,MySql的ping检测优化。Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,这是一个手写的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象语法树很方便。简单SQL语句用时10微秒以内,复杂SQL用时30微秒。通过Druid提供的SQL Parser可以在JDBC层拦截SQL做相应处理,比如说分库分表、审计等。Druid防御SQL注入攻击的WallFilter就是通过Druid的SQL Parser分析语义实现的。 |
||||
属性对比 |
|||||
连接池的配置大体可以分为:基本配置、关键配置、性能配置等主要配置 | |||||
基本配置:连接池进行数据库连接的四个必须配置 |
|||||
基 本 配 置 |
DBPC | C3P0 | Druid | 基 本 配 置 |
|
用户名 | username | uer | username | ||
密码 | password | password | password | ||
URL | url | jdbcUrl | jdbcUrl | ||
驱动类名 | driverClassName | driverClass | driverClassName | ||
注:在Druid连接池配置中,driverClassName可配可不配,不配置的话可以根据url自动识别数据库类型,然后选择相应的driverClassName。 | |||||
关键配置:为了发挥数据库连接池的作用。 | |||||
关 键 配 置 |
DBCP | c3p0 | Druid | 关 键 配 置 |
|
最小连接数 | minldle(0) | miniPoolSize(3) | minldle(0) | ||
初始化连接数 | innitialSize(0) | initialPoolSize(3) | initialSize | ||
最大连接数 | maxTotal(8) | maxPoolSize(15) | maxActive(8) | ||
最大等待时间 | maxWaitMillis(毫秒) | maxIdleTime(0秒) | maxWait(毫秒) | ||
说 明 |
最小连接数 | 是数据库一直保持的数据库连接数 | |||
初始化连接数 | 连接池启动时创建的初始化数据库连接数量 | ||||
最大连接数 | 连接池能申请的最大连接数,请求超出此数时,后面的数据库连接请求被加入等待队列中。 | ||||
最大等待时间 | 当没有可用连接时,连接池等待连接被归还的最大时间,超过时间则抛出异常,可设置为0或负数,无限等待。 | ||||
注 | 在DBCP连接池的配置中,还有一个maxldle的属性,表示最大空闲连接数,超过的空闲连接将被释放。对应的该属性在Druid中不再使用,配置了也不会有效果;而c3p0就没有对应的属性。 | ||||
注 | 数据库连接池在初始化的时候回创建initialSize个连接,当有数据库操作时,会从池中取出一个连接。如果连接数等于maxActive,则会等待一段时间,等待其他操作释放掉一个连接,如果这个时间超过了maxWait,就会报错如果当前使用的数量没有达到maxActive,则会判断当前是否空闲连接,有的话,直接使用空闲连接,没有的话,则新建一个连接。连接使用完毕后,放入池中,等待其他操作复用。 | ||||
性能配置:预缓存设置、连接有效性检测设置、连接超时关闭设置 | |||||
预缓存设置:用于控制PreparedStatement数量,提升数据库性能。 | 性 能 配 置 |
||||
预 缓 存 |
DBCP | c3p0 | Druid | ||
开启缓存功能 | poolPreparedStatements | maxStatements | poolPreparedStatements | ||
单个连接拥有的最大缓存数 | maxOpenPreparedStatements | maxStatementsPerConnection | maxOpenPreparedStatements | ||
连接有效性检测设置:连接池内部有机制判断,如果当前的总连接数少于minildle,则会建立新的空闲连接,以保证连接数达到minildle。如果当前连接池中某个连接处于空闲,则被物理性的关闭掉。有些数据库连接的时候有超时的限制(mysql连接8小时后断开),或者由于网络中断等原因,连接池的连接会出现失效,这时候,设置一个testWhileldle参数为true,可以保证连接池中,定时检测连接可用性,不可用的连接会被抛出或者重建,保证池中connection可用。 | |||||
连 接 有 效 性 检 测 设 置 |
DBCP | c3p0 | Druid | ||
申请连接检测 | testOnBorrow | testConnectionOnCheckin | testOnBorrow | ||
是否超时检测 | testWhileldle | testWhileldle | |||
空闲时间 | timeBetweenEvictionRuns Millis | idleConnectionTestPeriod | timeBetweenEvictionRunsMillis | ||
校验sql语句 | validationQuery | preferredTestQuery | validationQuery | ||
归还连接检测 | testOnReturn | testConnectionOnCheckout | testOnReturn | ||
超时连接关闭设置:用来检测当前使用的连接是否发生泄漏,所以在代码内部就假定如果一个连接建立连接时间很长,则认定为泄漏,继而强制关闭。 | |||||
超 时 连 接 关 闭 设 置 |
DBCP | c3p0 | Druid | ||
是否超时关闭连接 | removeAbandoned | breakAfterAcquireFailure | removeAbandoned | ||
超时时间 | removeAbandonedTimeout | checkoutTimeout | removeAbandonedTimeout | ||
是否记录日志 | logAbandoned | logAbandoned | |||
配置详解 | |||||
DBCP 属性说明表 | |||||
属性(Parameter) | 默认值(Default) | 描述(Description) | |||
username | 传递给JDBC驱动的用于建立连接的用户名(The connection username to be passed to our JDBC driver to establish a connection.) | ||||
password | 传递给JDBC驱动的用于建立连接的密码(The connection password to be passed to our JDBC driver to establish a connection.) | ||||
url | 传递给JDBC驱动的用于建立连接的URL(The connection URL to be passed to our JDBC driver to establish a connection.) | ||||
driverClassName | 使用的JDBC驱动的完整有效的java 类名(The fully qualified Java class name of the JDBC driver to be used.) | ||||
defaultAutoCommit | driver default | 连接池创建的连接的默认的auto-commit状态,没有设置则不会自动提交(The default auto-commit state of connections created by this pool. If not set then the setAutoCommit method will not be called.) | |||
initialSize | 0 | 初始化连接:连接池启动时创建的初始化连接数量(The initial number of connections that are created when the pool is started. | |||
maxTotal | 8 | 最大活动连接:连接池在同一时间能够分配的最大活动连接的数量, 如果设置为非正数则表示不限制(The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.) | |||
maxIdle | 8 | 最大空闲连接:连接池中容许保持空闲状态的最大连接数量,超过的空闲连接将被释放,如果设置为负数表示不限制(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.) | |||
minIdle | 0 | 最小空闲连接:连接池中容许保持空闲状态的最小连接数量,负数表示没有现在(The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.) | |||
注意:如果在某些负载比较大的系统中将maxIdel设置过小时,很可能会出现连接关闭的同时新连接马上打开的情况.这是由于关闭连接的线程比打开的快导致的.所以,对于这种系统中,maxIdle的设定值是不同的但是通常首选默认值 | |||||
(NOTE: If maxIdle is set too low on heavily loaded systems it is possible you will see connections being closed and almost immediately new connections being opened. This is a result of the active threads momentarily closing connections faster than they are opening them, causing the number of idle connections to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.) | |||||
maxWaitMillis | indefinitely | 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常,如果设置为-1表示无限等待(The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.) | |||
validationQuery | SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录(The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row. If not specified, connections will be validation by calling the isValid() method.) | ||||
testOnCreate | FALSE | 指明是否在建立连接之后进行验证,如果验证失败,则尝试重新建立连接(The indication of whether objects will be validated after creation. If the object fails to validate, the borrow attempt that triggered the object creation will fail.) | |||
testOnBorrow | TRUE | 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个. 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串(The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.) | |||
testOnReturn | FALSE | 指明是否在归还到池中前进行检验(The indication of whether objects will be validated before being returned to the pool.) | |||
testWhileIdle | FALSE | 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除. 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串(The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.) | |||
timeBetweenEviction-RunsMillis | -1 | 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位.如果设置为非正数,则不运行空闲连接回收器线程(The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.) | |||
numTestsPerEvictionRun | 3 | 在每次空闲连接回收器线程(如果有)运行时检查的连接数量(The number of objects to examine during each run of the idle object evictor thread (if any).) | |||
minEvictableIdleTime-Millis | 1000*60*30 | 连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒(The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).) | |||
softMiniEvictableIdle- TimeMillis | -1 | 说明(The minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle connection evictor, with the extra condition that at least "minIdle" connections remain in the pool. When miniEvictableIdleTimeMillis is set to a positive value, miniEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are visited by the evictor, idle time is first compared against miniEvictableIdleTimeMillis (without considering the number of idle connections in the pool) and then against softMinEvictableIdleTimeMillis, including the minIdle constraint.) | |||
maxConnLifetimeMillis | -1 | 说明(The maximum lifetime in milliseconds of a connection. After this time is exceeded the connection will fail the next activation, passivation or validation test. A value of zero or less means the connection has an infinite lifetime.) | |||
logExpiredConnections | TRUE | 说明(Flag to log a message indicating that a connection is being closed by the pool due to maxConnLifetimeMillis exceeded. Set this property to false to suppress expired connection logging that is turned on by default. | |||
connectionInitSqls | null | 说明(A Collection of SQL statements that will be used to initialize physical connections when they are first created. These statements are executed only once - when the configured connection factory creates the connection.) | |||
info | TRUE | 说明(True means that borrowObject returns the most recently used ("last in") connection in the pool (if there are idle connections available). False means that the pool behaves as a FIFO queue - connections are taken from the idle instance pool in the order that they are returned to the pool.) | |||
poolPreparedState-ments | FALSE | 开启池的prepared statement 池功能(Enable prepared statement pooling for this pool.) | |||
maxOpenPreparedState-ments | unlimited | statement池能够同时分配的打开的statements的最大数量, 如果设置为0表示不限制(The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.) | |||
NOTE - Make sure your connection has some resources left for the other statements. Pooling PreparedStatements may keep their cursors open in the database, causing a connection to run out of cursors, especially if maxOpenPreparedStatements is left at the default (unlimited) and an application opens a large number of different PreparedStatements per connection. To avoid this problem, maxOpenPreparedStatements should be set to a value less than the maximum number of cursors that can be open on a Connection. | |||||
accessToUnderlyingConnectionAllowed | FALSE | 控制PoolGuard是否容许获取底层连接(Controls if the PoolGuard allows access to the underlying connection.) 默认false不开启, 这是一个有潜在危险的功能, 不适当的编码会造成伤害.(关闭底层连接或者在守护连接已经关闭的情况下继续使用它).请谨慎使用,并且仅当需要直接访问驱动的特定功能时使用.注意: 不要关闭底层连接, 只能关闭前面的那个. Default is false, it is a potential dangerous operation and misbehaving programs can do harmful things. (closing the underlying or continue using it when the guarded connection is already closed) Be careful and only use when you need direct access to driver specific extensions. NOTE: Do not close the underlying connection, only the original one. | |||
removeAbandoned | FALSE | 标记是否删除泄露的连接,如果他们超过了removeAbandonedTimout的限制.如果设置为true, 连接被认为是被泄露并且可以被删除,如果空闲时间超过removeAbandonedTimeout. 设置为true可以为写法糟糕的没有关闭连接的程序修复数据库连接. (Flags to remove abandoned connections if they exceed the removeAbandonedTimout. A connection is considered abandoned and eligible for removal if it has not been used for longer than removeAbandonedTimeout. Setting one or both of these to true can recover db connections from poorly written applications which fail to close connections.) | |||
removeAbandonedTimeout | 300 | 泄露的连接可以被删除的超时值, 单位秒(Timeout in seconds before an abandoned connection can be removed.) | |||
logAbandoned | FALSE | 标记当Statement或连接被泄露时是否打印程序的stack traces日志。被泄露的Statements和连接的日志添加在每个连接打开或者生成新的Statement,因为需要生成stack trace。(Flag to log stack traces for application code which abandoned a Statement or Connection. Logging of abandoned Statements and Connections adds overhead for every Connection open or new Statement because a stack trace has to be generated.) | |||
abandonedUsageTracking | FALSE | 如果为true, 那么连接池会记录每个方法调用时候的堆栈信息以及废弃连接的调试信息(If true, the connection pool records a stack trace every time a method is called on a pooled connection and retains the most recent stack trace to aid debugging of abandoned connections. There is significant overhead added by setting this to true.) | |||
注:如果开启"removeAbandoned",那么连接在被认为泄露时可能被池回收. 这个机制在(getNumIdle() < 2)and (getNumActive() > getMaxActive() - 3)时被触发. 举例当maxActive=20, 活动连接为18,空闲连接为1时可以触发"removeAbandoned".但是活动连接只有在没有被使用的时间超过"removeAbandonedTimeout"时才被删除,默认300秒.在resultset中游历不被计算为被使用. | |||||
If you have enabled removeAbandonedOnMaintenance or removeAbandonedOnBorrow then it is possible that a connection is reclaimed by the pool because it is considered to be abandoned. This mechanism is triggered when (getNumIdle() < 2) and (getNumActive() > getMaxTotal() - 3) and removeAbandonedOnBorrow is true; or after eviction finishes and removeAbandonedOnMaintenance is true. For example, maxTotal=20 and 18 active connections and 1 idle connection would trigger removeAbandonedOnBorrow, but only the active connections that aren't used for more then "removeAbandonedTimeout" seconds are removed (default 300 sec). Traversing a resultset doesn't count as being used. Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one of the execute methods) resets the lastUsed property of the parent connection. | |||||
C3P0 属性说明表 | |||||
属性(Parameter) | 默认值(Default) | 描述(Description) | |||
user | 同DBCP中的username属性 | ||||
password | 同DBCP中的password属性 | ||||
jdbcUrl | 同DBCP中的jdbcUrl属性 | ||||
driverClass | 同DBCP中的driverClass属性 | ||||
autoCommitOnClose | FALSE | 默认值false表示回滚任何未提交的任务,设置为true则全部提交,而不是在关闭连接之前回滚(C3P0's default policy is to rollback any uncommitted, pending work. Setting autoCommitOnClose to true causes uncommitted pending work to be committed, rather than rolled back on Connection close.)*参见DBCP中的defaultAutoCommit属性 | |||
initialPoolSize | 3 | 初始化连接:连接池启动时创建的初始化连接数量(The initial number of connections that are created when the pool is started.*参见DBCP中的initialSize属性 | |||
maxPoolSize | 15 | 连接池中保留的最大连接数(Maximum number of Connections a pool will maintain at any given time.) *参见DBCP中的maxIdle属性 | |||
minPoolSize | 3 | 连接池中保留的最小连接数(Minimum number of Connections a pool will maintain at any given time.) *参见DBCP中的maxIdle属性 | |||
maxIdleTime | 0 | 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以秒计数),超过时间则抛出异常,如果设置为0表示无限等待(Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.) *参见DBCP中maxWaitMillis 属性 | |||
preferredTestQuery | null | 定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。(Defines the query that will be executed for all connection tests, if the default ConnectionTester (or some other implementation of QueryConnectionTester, or better yet FullQueryConnectionTester) is being used. Defining a preferredTestQuery that will execute quickly in your database may dramatically speed up Connection tests.) | |||
testConnectionOn- Checkin | FALSE | 如果设为true那么在取得连接的同时将校验连接的有效性。(If true, an operation will be performed asynchronously at every connection checkin to verify that the connection is valid. Use in combination with idleConnectionTestPeriod for quite reliable, always asynchronous Connection testing.) *参见DBCP中的testOnBorrow属性 | |||
testConnectionOn- Checkout | FALSE | 如果设为true那么在每个connection提交的时候都将校验其有效性,但是要确保配置的preferredTestQuery的有效性(If true, an operation will be performed at every connection checkout to verify that the connection is valid. Be sure to set an efficient preferredTestQuery or automaticTestTable if you set this to true.) *参见DBCP中的testOnBorrow属性 | |||
idleConnectionTest- Period | 0 | 如果设置大于0,表示过了多少秒检查一次空闲连接,结合testConnectionOnCheckin以及testConnectionOnCheckout使用(If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.) | |||
acquireRetryAttempts | 30 | 定义在从数据库获取新连接失败后重复尝试的次数, 如果小于0则表示无限制的连接。(Defines how many times c3p0 will try to acquire a new Connection from the database before giving up. If this value is less than or equal to zero, c3p0 will keep trying to fetch a Connection indefinitely.) | |||
acquireRetryDelay | 1000 | 两次连接中的间隔时间,单位毫秒。(Milliseconds, time c3p0 will wait between acquire attempts.) | |||
breakAfterAcquire-Failure | FALSE | 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用 getConnection() 的时候继续尝试获取连接。如果为 true,那么在尝试获取连接失败后该数据源将声明已断开并永久关闭。(If true, a pooled DataSource will declare itself broken and be permanently closed if a Connection cannot be obtained from the database after making acquireRetryAttempts to acquire one. If false, failure to obtain a Connection will cause all Threads waiting for the pool to acquire a Connection to throw an Exception, but the DataSource will remain valid, and will attempt to acquire again following a call to getConnection().) | |||
checkoutTimeout | 0 | 当连接池用完时客户端调用getConnection() 后等待获取新连接的时间,潮湿后将抛出SQLException,如设为0,则为无限期等待。单位毫秒。(The number of milliseconds a client calling getConnection() will wait for a Connection to be checked-in or acquired when the pool is exhausted. Zero means wait indefinitely. Setting any positive value will cause the getConnection() call to time-out and break with an SQLException after the specified number of milliseconds.) | |||
maxStatements | 0 | 控制数据源内加载的PreparedStatements数量(Enable prepared statement pooling for this pool.) | |||
maxStatementsPer- Connection | 0 | 定义了连接池内单个连接所拥有的最大缓存statements数(The maximum number of open statements that can be allocated from the statement pool at the same time, or negative for no limit.) | |||
DRUID 属性说明表 | |||||
属性(Parameter) | 默认值(Default) | 描述(Description) | |||
username | 连接数据库的用户名 | ||||
password | 连接数据库的密码 | ||||
jdbcUrl | 同DBCP中的jdbcUrl属性 | ||||
driverClassName | 根据url自动识别 | 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName | |||
initialSize | 0 | 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 *参见DBCP中的initialSize属性 | |||
maxActive | 8 | 最大连接池数量(Maximum number of Connections a pool will maintain at any given time.) *参见DBCP中的maxTotal属性 |
|||
maxIdle | 8 | 已经不再使用,配置了也没效果 *参见DBCP中的maxIdle属性 |
|||
minIdle | 最小连接池数量 | ||||
maxWait | 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 | ||||
poolPreparedState- ments | FALSE | 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。 | |||
maxOpenPrepared- Statements | -1 | 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 | |||
testOnBorrow | TRUE | 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 | |||
testOnReturn | FALSE | 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 | |||
testWhileIdle | FALSE | 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 | |||
validationQuery | 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。在mysql中通常为select 'x',在oracle中通常为select 1 from dual | ||||
timeBetweenEviction-RunsMillis | 1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据 |
||||
minEvictableIdle- TimeMillis | Destory线程中如果检测到当前连接的最后活跃时间和当前时间的差值大于minEvictableIdleTimeMillis,则关闭当前连接。 | ||||
removeAbandoned | 对于建立时间超过removeAbandonedTimeout的连接强制关闭 | ||||
removeAbandoned-Timeout | 指定连接建立多长时间就需要被强制关闭 | ||||
logAbandoned | FALSE | 指定发生removeabandoned的时候,是否记录当前线程的堆栈信息到日志中 | |||
filters | 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 1)监控统计用的filter:stat 2)日志用的filter:log4j 3)防御sql注入的filter:wall |
性能比较
最大连接数控制为10,测试获取10w个连接的时间,单位为ms | |||
这个表是对从第一次获取连接开始至最后次的统计时间(total) | |||
次 | dbcp | druid | C3P0 |
1 | 8475 | 655 | 1621 |
2 | 8333 | 793 | 1732 |
3 | 8291 | 753 | 1745 |
4 | 8454 | 642 | 1581 |
5 | 8464 | 673 | 1848 |
6 | 8307 | 660 | 1787 |
7 | 8337 | 699 | 1808 |
8 | 8438 | 581 | 1707 |
平均该表为每次获取连接时间的统计(sum) | |||
次 | dbcp | druid | C3P0 |
1 | 8305 | 579 | 1370 |
2 | 8124 | 698 | 1443 |
3 | 8140 | 665 | 1514 |
4 | 8284 | 575 | 1331 |
5 | 8278 | 596 | 1463 |
6 | 8127 | 572 | 1490 |
7 | 8155 | 606 | 1522 |
8 | 8229 | 509 | 1439 |
平均对比三者的性能,druid最快,c3p0还行,dbcp我就不吐槽了和其他两个不在一个数量级上。 | |||
关键功能 | |||
Druid | DBCP | C3P0 | |
LRU | 是 | 是 | 否 |
PSCache | 是 | 是 | 是 |
PSCache-Oracle-Optimized | 是 | 否 | 否 |
ExceptionSorter | 是 | 否 | 否 |
监控 | 是 | 否 | 否 |
扩展 | 是 | 否 | 否 |
对比功能发现,druid的确强大,特别扩展功能这块,druid可以扩展很多插件,包括扩展其他连接池。 | |||
对比三个连接池发现,druid功能强大,性能高,虽然在复杂环境下稳定性没有测过,但是后援团(阿里)比较强大,就算有问题也能很快的解决。而且还提供了监控平台,有助于优化我们的代码和sql。并且在扩展上支持在连接池层面的sql注入预警。 |
欢迎留言,小编会第一时间整理你要的资源与技术文章
以上是关于c3p0,DBPC,Druid三大连接池的区别/性能面试+工作的主要内容,如果未能解决你的问题,请参考以下文章