欢迎您光临本小站。希望您在这里可以找到自己想要的信息。。。

各种Java技术框架数据库连接池比较

Java Web water 4423℃

现在常用的开源数据连接池主要有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为

    <JDBCConnectionPool
        ConnectionCreationRetryFrequencySeconds=”30″
        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], 现摘录如下:

<c3p0-config>
<default-config>
<!–当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 –>
<property name=”acquireIncrement”>3</property>

<!–定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 –>
<property name=”acquireRetryAttempts”>30</property>

<!–两次连接中间隔时间,单位毫秒。Default: 1000 –>
<property name=”acquireRetryDelay”>1000</property>

<!–连接关闭时默认将所有未提交的操作回滚。Default: false –>
<property name=”autoCommitOnClose”>false</property>

<!–c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么
属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试
使用。Default: null–>
<property name=”automaticTestTable”>Test</property>

<!–获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false–>
<property name=”breakAfterAcquireFailure”>false</property>

<!–当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
SQLException,如设为0则无限期等待。单位毫秒。Default: 0 –>
<property name=”checkoutTimeout”>100</property>

<!–通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。
Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester–>
<property name=”connectionTesterClassName”></property>

<!–指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可
Default: null–>
<property name=”factoryClassLocation”>null</property>

<!–Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs.
(文档原文)作者强烈建议不使用的一个属性–>
<property name=”forceIgnoreUnresolvedTransactions”>false</property>

<!–每60秒检查所有连接池中的空闲连接。Default: 0 –>
<property name=”idleConnectionTestPeriod”>60</property>

<!–初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 –>
<property name=”initialPoolSize”>3</property>

<!–最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 –>
<property name=”maxIdleTime”>60</property>

<!–连接池中保留的最大连接数。Default: 15 –>
<property name=”maxPoolSize”>15</property>

<!–JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0–>
<property name=”maxStatements”>100</property>

<!–maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 –>
<property name=”maxStatementsPerConnection”></property>

<!–c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能
通过多线程实现多个操作同时被执行。Default: 3–>
<property name=”numHelperThreads”>3</property>

<!–当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0
的数据源时。Default: null–>
<property name=”overrideDefaultUser”>root</property>

<!–与overrideDefaultUser参数对应使用的一个参数。Default: null–>
<property name=”overrideDefaultPassword”>password</property>

<!–密码。Default: null–>
<property name=”password”></property>

<!–定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:
测试的表必须在初始数据源的时候就存在。Default: null–>
<property name=”preferredTestQuery”>select id from test where id=1</property>

<!–用户修改系统配置参数执行前最多等待300秒。Default: 300 –>
<property name=”propertyCycle”>300</property>

<!–因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false –>
<property name=”testConnectionOnCheckout”>false</property>

<!–如果设为true那么在取得连接的同时将校验连接的有效性。Default: false –>
<property name=”testConnectionOnCheckin”>true</property>

<!–用户名。Default: null–>
<property name=”user”>root</property>

<!–早期的c3p0版本对JDBC接口采用动态反射代理。在早期版本用途广泛的情况下这个参数
允许用户恢复到动态反射代理以解决不稳定的故障。最新的非反射代理更快并且已经开始
广泛的被使用,所以这个参数未必有用。现在原先的动态反射与新的非反射代理同时受到
支持,但今后可能的版本可能不支持动态反射代理。Default: false–>
<property name=”usesTraditionalReflectiveProxies”>false</property>

<property name=”automaticTestTable”>con_test</property>
    <property name=”checkoutTimeout”>30000</property>
    <property name=”idleConnectionTestPeriod”>30</property>
    <property name=”initialPoolSize”>10</property>
    <property name=”maxIdleTime”>30</property>
    <property name=”maxPoolSize”>25</property>
    <property name=”minPoolSize”>10</property>
    <property name=”maxStatements”>0</property>
    <user-overrides user=”swaldman”>
    </user-overrides>
</default-config>
<named-config name=”dumbTestConfig”>
    <property name=”maxStatements”>200</property>
    <user-overrides user=”poop”>
      <property name=”maxStatements”>300</property>
    </user-overrides>
   </named-config>
</c3p0-config>

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=<alias>”注册为JMS Server的MBean。默认值为false

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”);方式设定

? 上述特性的可设置性已在代码中验证,具体性能是否能实现有待实际验证

转载请注明:学时网 » 各种Java技术框架数据库连接池比较

喜欢 (0)or分享 (0)