分类: Java
2014-05-08 16:28:27
现在常用的开源数据连接池主要有c3p0,dbcp和proxool三种,其中:
¨ hibernate开发组推荐使用c3p0;
¨ spring开发组推荐使用dbcp (dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法reconnect ,告诉连接被重置,这个设置可以解决);
¨ hibernate in action推荐使用c3p0和proxool;
下面具体就每种连接池的调研结果进行说明:
1. Apache-DBCP
? BasicDataSource 相关的参数说明
dataSource: 要连接的 datasource (通常我们不会定义在 server.xml)
defaultAutoCommit: 对于事务是否 autoCommit, 默认值为 true
defaultReadOnly: 对于数据库是否只能读取, 默认值为 false
driverClassName:连接数据库所用的 JDBC Driver Class,
maxActive: 可以从对象池中取出的对象最大个数,为0则表示没有限制,默认为8
maxIdle: 最大等待连接中的数量,设 0 为没有限制 (对象池中对象最大个数)
minIdle:对象池中对象最小个数
maxWait: 最大等待秒数, 单位为 ms, 超过时间会丟出错误信息
password: 登陆数据库所用的密码
url: 连接数据库的 URL
username: 登陆数据库所用的帐号
validationQuery: 验证连接是否成功, SQL SELECT 指令至少要返回一行
removeAbandoned: 是否自我中断, 默认是 false
removeAbandonedTimeout: 几秒后会自我中断, removeAbandoned 必须为 true
logAbandoned: 是否记录中断事件, 默认为 false
minEvictableIdleTimeMillis:大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证;默认30分钟
timeBetweenEvictionRunsMillis:失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程,默认-1
testOnBorrow:取得对象时是否进行验证,检查对象是否有效,默认为false
testOnReturn:返回对象时是否进行验证,检查对象是否有效,默认为false
testWhileIdle:空闲时是否进行验证,检查对象是否有效,默认为false
? 在使用DBCP的时候,如果使用默认值,则数据库连接因为某种原因断掉后,再从连接池中取得连接又不进行验证,这时取得的连接实际上就会是无效的数据库连接。因此为了防止获得的数据库连接失效,在使用的时候最好保证:
username: 登陆数据库所用的帐号
validationQuery:SELECT COUNT(*) FROM DUAL
testOnBorrow、testOnReturn、testWhileIdle:最好都设为true
minEvictableIdleTimeMillis:大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证
timeBetweenEvictionRunsMillis:失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程
? PS:在构造GenericObjectPool [BasicDataSource在其createDataSource () 方法中也会使用GenericObjectPool] 时,会生成一个内嵌类Evictor,实现自Runnable接口。如果timeBetweenEvictionRunsMillis大于0,每过timeBetweenEvictionRunsMillis毫秒Evictor会调用evict()方法,检查对象的闲置时间是否大于 minEvictableIdleTimeMillis毫秒(_minEvictableIdleTimeMillis小于等于0时则忽略,默认为30分钟),是则销毁此对象,否则就激活并校验对象,然后调用ensureMinIdle方法检查确保池中对象个数不小于_minIdle。在调用returnObject方法把对象放回对象池,首先检查该对象是否有效,然后调用PoolableObjectFactory 的passivateObject方法使对象处于非活动状态。再检查对象池中对象个数是否小于maxIdle,是则可以把此对象放回对象池,否则销毁此对象
? 上述特性的可设置性已在代码中验证,具体性能是否能实现有待实际验证
Weblogic的DBPool连接测试配置方法:
进入某个连接池配置页,进入“连接“页,点击高级选项的”show“显示高级选项。
指定 "测试频率" 并启用 "测试保留的连接"、"测试创建的连接" 和 "测试释放的连接"。
测试频率(Test Frequency): 60 秒
自动数据库连接测试之间的秒数(0 - 32 位正整数)。测试失败的连接将被关闭,然后重新打开以重新建立有效的物理数据库连接。(必须在下面指定测试表名称。)
The number of seconds (between 0 and a positive 32-bit integer) between automatic database connection tests. Connections that fail the test are closed and reopened to re-establish a valid physical database connection. (You must specify a Test Table Name below.)
测试保留的连接(Test Reserved Connections)
指定 WebLogic Server 是否在将连接提供给客户端之前测试该连接。(必须在下面指定测试表名称。)
Specifies whether WebLogic Server tests a connection before giving it to the client. (You must specify a Test Table Name below.)
测试创建的连接(Test Created Connections)
指定 WebLogic Server 是否在创建连接后且将该连接添加到缓冲池中可用连接列表之前对该连接进行测试。(必须指定测试表名称。)
Specifies whether WebLogic Server tests a connection after creating it but before adding it to the list of connections available in the pool. (You must specify a Test Table Name.)
测试释放的连接(Test Released Connections)
指定 WebLogic Server 是否在将连接返回到该 JDBC 连接缓冲池之前测试该连接。(必须指定测试表名称。)
Specifies whether WebLogic Server tests a connection before returning it to this JDBC connection pool. (You must specify a Test Table Name.)
连接保留超时(Connection Reserve Timeout): 设为 30 秒
在保留缓冲池连接的调用超时之前的秒数(-1 - 32 位正整数)。如果设置为 -1,则调用永远不会超时。
The number of seconds (between -1 and a positive 32-bit integer) after which a call to reserve a connection from the pool will timeout. When set to 0, a call will never timeout. When set to -1, a call will timeout immediately
重试创建连接的频率(Connection Creation Retry Frequency): 设为 30 秒
尝试建立与数据库的连接的间隔秒数(0 - 32 位正整数)。适用于在数据库不可用的情况下服务器启动时创建的连接缓冲池。
The number of seconds (between 0 and a positive 32-bit integer) between attempts to establish connections to the database. Applies to connection pools created at server startup when the database is unavailable.
钝化连接超时(Inactive Connection Timeout):
The number of inactive seconds on a reserved connection (between 0 and a positive 32-bit integer) before WebLogic Server reclaims the connection and releases it back into the connection pool.
服务器申请复议连接并且保留返回给连接池前的钝化时间
测试表名称(Test Table Name):
oracle的连接池默认为 SQL SELECT 1 FROM DUAL,sybase数据库可以填入一个记录不多的配置表名例如sys_code。
The name of the database table to use when testing physical database connections. This field is required when you specify a Test Frequency and enable Test Reserved Connections, Test Created Connections, and Test Released Connections
以上属性对应config.xml为
ConnectionReserveTimeoutSeconds="30"
InactiveConnectionTimeoutSeconds="60"
SecondsToTrustAnIdlePoolConnection="30"
TestConnectionsOnCreate="true"
TestConnectionsOnRelease="true"
TestConnectionsOnReserve="true"
TestFrequencySeconds="300"
TestTableName="SQL SELECT 1 FROM DUAL"
......
/>
2. C3P0
? C3P0的官方example中使用的数据源为ComboPooledDataSource,网上一篇文章详细介绍了C3P0连接池配置中各项含义[这些配置项的含义在下载解压c3p0的压缩包之后目录的doc\index.html中的Configuration部分也有详细的介绍,这里偷下懒:P],现摘录如下:
3. Proxool
? Proxool的使用和dbcp以及c3p0稍有不同,我们需要并且只需要在使用基本的java.sql.DriverManager之前加载org.logicalcobwebs.proxool.ProxoolDriver驱动类,并且按照proxool定义的url格式 ["proxool." + alias + ":" + driverClass + ":" + driverUrl ,其中alias是为连接池自定义的别名] 来获得connection;具体的可以参看proxool doc下的UserGuide,或本文所附的示例代码。下面对连接池的特性配置作详细说明 [这个是自己翻译的,不一定准确,有问题时请参看doc下的Properties ~]。
n fatal-sql-exception
以逗号隔开的异常列表,当设置了此项之后,每当出现SQLException时都将与列表中异常项作比较,如果匹配则认为出现fatal异常,这将导致connection被丢弃,并且不论出现任何情况该异常将被重抛一次以通知用户发生的情况。默认值为null
n fatal-sql-exception-wrapper-class
如果配置了fatal-sql-exception,则默认的操作是丢弃引起SQLException的原因而只是抛出原始异常。使用fatal-sql-exception-wrapper-class这个特性可以将SQLException包装到继承SQLException或RunTimeException的任何异常类里。Proxool提供了两个类供使用FatalSQLException和FatalRunTimeException;使用这两个类的话就将该选项设置为'org.logicalcobwebs.proxool.FatalSQLException'或者'org.logicalcobwebs.proxool.FatalRuntimeException'。默认值为null
n house-keeping-sleep-time
proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁,默认值为30秒
n house-keeping-test-sql
如果侦察线程发现闲置连接,则会使用这个SQL语句来对这些连接进行检查;这项设置的语句应该能够被很快的执行,例如查询当前时间 [info.setProperty("proxool.house-keeping-test-sql", "select CURRENT_DATE");] 。如果不设置则该选项被忽略
n injectable-connection-interface、injectable-statement-interface、injectable-prepared-statement-interface、injectable-callable-statement-interface
n jmx
如果此项设为true,则连接池将被以名称"Proxool:type=Pool, name=
n jmx-agent-id
当且仅当jmx选项设为true时使用,为以逗号分隔的连接持注册到的JMS代理名称列表;如果不设置则所有注册的JMX Server都将被使用
n maximum-active-time
线程最大存活时间,超过此时间的线程将被守护线程kill掉,默认值为5分钟
n maximum-connection-count
到数据库的最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由simultaneous-build-throttle决定;默认值为15
n maximum-connection-lifetime
连接最大存活时间,毫秒为单位,默认值为4小时
n minimum-connection-count
不管是否被使用都保持开放的最小连接数,默认值为5
n overload-without-refusal-lifetime
用来判断连接池状态,如果在此选项设置时间内(毫秒为单位)拒绝了连接,则认为过负载。默认值为60秒
n prototype-count
最少保持的空闲连接数,注意与minimum-connection-count区分。默认值为0
n simultaneous-build-throttle
最大的等待请求数,默认值为10
n test-before-use
如果设为true则connection在使用前将以house-keeping-test-sql设置的语句测试,如果测试不通过则该connection被丢弃并会重新分配一个connection。默认为false
n test-after-use
如果设为true则connection在关闭(放回连接池)前将以house-keeping-test-sql设置的语句测试,如果测试不通过connection将被丢弃。默认值为false
? 与其它连接池特性的设置方法不同,Proxool不提供相应的set方法,所有特性都要以诸如info.setProperty("proxool.jmx", "false");方式设定
? 上述特性的可设置性已在代码中验证,具体性能是否能实现有待实际验证