原文:http://blog.csdn.net/xby1993/article/details/23707775html
c3p0号称是java界最好的数据池。java
c3p0的配置方式分为三种,分别是mysql
1.setters一个个地设置各个配置项sql
2.类路径下提供一个c3p0.properties文件数据库
3.类路径下提供一个c3p0-config.xml文件缓存
咱们主要说下c3p0-config.xml配置:tomcat
<c3p0-config>服务器
//默认池配置网络
<default-config> 多线程
<property name="user">root</property>
<property name="password">java</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
//命名池配置
<named-config name="myApp">
<property name="user">root</property>
<property name="password">java</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</named-config>
</c3p0-config>
可见c3p0能够配置在一个应用中使用多个池。换言之可使用多个数据库。
使用:
//使用默认池:
private static ComboPooledDataSource ds = new ComboPooledDataSource();
//使用命名池
private static ComboPooledDataSource ds = new ComboPooledDataSource("myApp");
基本配置项的介绍:
1.基本配置
acquireIncrement
default : 3
链接池在无空闲链接可用时一次性建立的新数据库链接数
initialPoolSize
default : 3
链接池初始化时建立的链接数
maxPoolSize
default : 15
链接池中拥有的最大链接数,若是得到新链接时会使链接总数超过这个值则不会再获取新链接,而是等待
其余链接释放,因此这个值有可能会设计地很大
maxIdleTime
default : 0 单位 s
链接的最大空闲时间,若是超过这个时间,某个数据库链接尚未被使用,则会断开掉这个链接
若是为0,则永远不会断开链接
minPoolSize
default : 3
链接池保持的最小链接数,后面的maxIdleTimeExcessConnections跟这个配合使用来减轻链接池的负载
2.管理链接池的大小和链接的生存时间
maxConnectionAge
default : 0 单位 s
配置链接的生存时间,超过这个时间的链接将由链接池自动断开丢弃掉。固然正在使用的链接不会立刻断开,而是等待
它close再断开。配置为0的时候则不会对链接的生存时间进行限制。
maxIdleTimeExcessConnections
default : 0 单位 s
这个配置主要是为了减轻链接池的负载,好比链接池中链接数由于某次数据访问高峰致使建立了不少数据链接
可是后面的时间段须要的数据库链接数不多,则此时链接池彻底没有必要维护那么多的链接,因此有必要将
断开丢弃掉一些链接来减轻负载,必须小于maxIdleTime。配置不为0,则会将链接池中的链接数量保持到minPoolSize。
为0则不处理。
3.配置链接测试:由于链接池中的数据库链接颇有多是维持数小时的链接,颇有可能由于数据库服务器的问题,网络问题等致使实际链接已经无效,可是链接池里面的链接仍是有效的,若是此时得到链接确定会发生异常,因此有必要经过测试链接来确认链接的有效性。
下面的前三项用来配置如何对链接进行测试,后三项配置对链接进行测试的时机。
automaticTestTable
default : null
用来配置测试链接的一种方式。配置一个表名,链接池根据这个表名建立一个空表,
而且用本身的测试sql语句在这个空表上测试数据库链接
这个表只能由c3p0来使用,用户不能操做,同时用户配置的preferredTestQuery 将会被忽略。
preferredTestQuery
default : null
用来配置测试链接的另外一种方式。与上面的automaticTestTable两者只能选一。
若是要用它测试链接,千万不要设为null,不然测试过程会很耗时,同时要保证sql语句中的表在数据库中必定存在。
connectionTesterClassName
default : com.mchange.v2.c3p0.impl.DefaultConnectionTester
链接池用来支持automaticTestTable和preferredTestQuery测试的类,必须是全类名,就像默认的那样,
能够经过实现UnifiedConnectionTester接口或者继承AbstractConnectionTester来定制本身的测试方法
idleConnectionTestPeriod
default : 0
用来配置测试空闲链接的间隔时间。测试方式仍是上面的两种之一,能够用来解决MySQL8小时断开链接的问题。由于它
保证链接池会每隔必定时间对空闲链接进行一次测试,从而保证有效的空闲链接能每隔必定时间访问一次数据库,将于MySQL
8小时无会话的状态打破。为0则不测试。
testConnectionOnCheckin
default : false
若是为true,则在close的时候测试链接的有效性。为了提升测试性能,能够与idleConnectionTestPeriod搭配使用,
配置preferredTestQuery或automaticTestTable也能够加快测试速度。
testConnectionOnCheckout
default : false
性能消耗大。若是为true,在每次getConnection的时候都会测试,为了提升性能,
能够与idleConnectionTestPeriod搭配使用,
配置preferredTestQuery或automaticTestTable也能够加快测试速度。
4.配置PreparedStatement缓存
maxStatements
default : 0
链接池为数据源缓存的PreparedStatement的总数。因为PreparedStatement属于单个Connection,因此
这个数量应该根据应用中平均链接数乘以每一个链接的平均PreparedStatement来计算。为0的时候不缓存,
同时maxStatementsPerConnection的配置无效。
maxStatementsPerConnection
default : 0
链接池为数据源单个Connection缓存的PreparedStatement数,这个配置比maxStatements更有意义,由于
它缓存的服务对象是单个数据链接,若是设置的好,确定是能够提升性能的。为0的时候不缓存。
注意当数据池再也不使用时须要调用close()方法手动关闭它配置举例:
解决MYSQL 8小时问题
Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。这就是问题的所在,在C3P0 pools中的connections若是空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,若是这时有 Client请求connection,C3P0将该失效的Connection提供给Client,将会形成上面的异常。
解决的方法有3种:
增长wait_timeout的时间。
减小Connection pools中connection的lifetime。
测试Connection pools中connection的有效性。
C3P0增长如下配置信息:
//自动测试的table名称
automaticTestTable=C3P0TestTable
//set to something much less than wait_timeout, prevents connections from going stale。每一个小时测试一次
idleConnectionTestPeriod = 3600
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out最大空闲时间为2个小时
maxIdleTime = 7200
//获取connnection时测试是否有效
testConnectionOnCheckin = true
总体说明:
<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>
补充一点,与c3p0配置无关,可是对于tomcat中配置DBCP很重要。
对于tomcat中的DBCP,也须要解决Mysql 8小时问题,因此须要这样:
//set to 'SELECT 1'
validationQuery = "SELECT 1"
//set to 'true'
testWhileIdle = "true"
//some positive integer
timeBetweenEvictionRunsMillis = 3600000
//set to something smaller than 'wait_timeout'
minEvictableIdleTimeMillis = 18000000
//if you don't mind a hit for every getConnection(), set to "true"
testOnBorrow = "true"
参考:http://my.oschina.net/lyzg/blog/55133
http://www.blogjava.net/Alpha/archive/2009/03/29/262789.html