c3g0连接池详解

本文档详细解析了Hibernate与C3P0连接池的集成配置,包括各种参数设置及其影响,如`acquireIncrement`、`idleConnectionTestPeriod`等。讨论了在Hibernate配置文件中如何覆盖C3P0的默认设置,并提供了示例配置,强调了在`hibernate.cfg.xml`中设置`hibernate.c3p0.*`属性的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以上基于 Hibernate Annotations 3.4.0.GA和Hibernate 3.3.1.GA的研究


1. 当hibernate.properties 和hibernate.cfg.xml同时配置了c3p0的相关属性,则hibernate会使用在hibernate.cfg.xml中的属性
加载hibernate日志的时候又这样一句话:Both hibernate-style property 'hibernate.c3p0.idle_test_period' and c3p0-style
property 'hibernate.c3p0.idleConnectionTestPeriod' have been set in hibernate.properties. Hibernate-style property
'hibernate.c3p0.idle_test_period' will be used and c3p0-style property 'hibernate.c3p0.idleConnectionTestPeriod'
will be ignored!
意思就是hibernate.c3p0.idle_test_period 和hibernate.c3p0.idleConnectionTestPeriod同时配置了,将使用hibernate.c3p0.idle_test_period

 

acquireIncrement -> 2,                                        连接不够用时,每次增长2个


acquireRetryAttempts -> 30,                               从数据库获取新连接失败后重复尝试的次数


acquireRetryDelay -> 1000,                                 两次连接获取中的间隔时间,单位毫秒


autoCommitOnClose -> false,                             连接关闭时默认将所有未提交的操作回滚


automaticTestTable -> c3p0TestTable,               c3p0将建一张名为c3p0TestTable的空表,并使用其自带的查询语句进行 试。如果定义了这个参数那么属性preferredTestQuery将被忽略。


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


checkoutTimeout -> 3000,                                    连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。

connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester,  通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径

factoryClassLocation -> null,                                 指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可

forceIgnoreUnresolvedTransactions -> false,     作者强烈建议不使用的一个属性

idleConnectionTestPeriod -> 3000000,               每60秒检查所有连接池中的空闲连接

initialPoolSize -> 5,                                               初始化时获取的连接数

maxConnectionAge -> 0,                                     强迫连接池扑杀任何的连接,这些连接来源于数据库并且超过了设置的秒数

maxIdleTime -> 50000,                                        最大空闲时间,50000秒内未使用则连接被丢弃。若为0则永不丢弃

maxIdleTimeExcessConnections -> 0,                 maxIdleTimeExcessConnections是关于当连接池在负载之下时,最小化被C3P0连接池拥有的连接数目

maxPoolSize -> 20,                                              最大的数据库连接池

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

maxStatementsPerConnection -> 0,

minPoolSize -> 5,                                                最小数据库连接池

preferredTestQuery -> null,                                定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。Default: null-->

propertyCycle -> 0,                                            用户修改系统配置参数执行前最多等待300秒

testConnectionOnCheckin -> true,                     如果设为true那么在取得连接的同时将校验连接的有效性

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

unreturnedConnectionTimeout -> 0,                  对连接是被检出的时间定义了限制(秒级)。如果设置为非零值,不返回的,

usesTraditionalReflectiveProxies -> false;          动态反射机制

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

c3p0在hibernate中的使用,摘自c3p0官网
Hibernate's C3P0ConnectionProvider renames 7 c3p0 configuration properties, which, if set in your hibernate configuration, will override any configuration you may have set in a c3p0.properties file:

c3p0-native property name                 hibernate configuration key
c3p0.acquireIncrement                     hibernate.c3p0.acquire_increment
c3p0.idleConnectionTestPeriod             hibernate.c3p0.idle_test_period
c3p0.initialPoolSize                      not available -- uses minimum size
c3p0.maxIdleTime                          hibernate.c3p0.timeout
c3p0.maxPoolSize                          hibernate.c3p0.max_size
c3p0.maxStatements                        hibernate.c3p0.max_statements
c3p0.minPoolSize                          hibernate.c3p0.min_size
c3p0.testConnectionOnCheckout             hibernate.c3p0.validate hibernate 2.x only!

You can set any c3p0 properties in your hibernate config using the prefix hibernate.c3p0. For example

hibernate.c3p0.unreturnedConnectionTimeout=30 hibernate.c3p0.debugUnreturnedConnectionStackTraces=true
might be set to help debug Connection leaks.

You can always set c3p0 config in a c3p0.properties or c3p0-config.xml file (see "Overriding c3p0 defaults via c3p0.properties", "Overriding c3p0 defaults via c3p0-config.xml"), but any configuration set in Hibernate config files will override c3p0-native configuation!

大致意思就是你可以在hibernate配置文件中设置c3p0的属性,前缀是hibernate.c3p0.你也可以将c3p0的属性设置在c3p0.properties或者c3p0-config.xml里,
但是任何在hibernate配置文件中的设置都将覆盖c3p0设置的属性。


在hibernate中的范例
hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="connection.url">jdbc:mysql://127.0.0.1:3306/users</property>
   <property name="connection.username">root</property>
   <property name="connection.password">unicom</property>
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     <!-- 配置C3P0连接池属性 -->  
     <!--下面这句话一定不能少,少了的话就无法配置使用c3p0连接池-->
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider </property>   
    <property name="hibernate.c3p0.max_size">20</property> 
    <property name="hibernate.c3p0.min_size">5</property> 
    <property name="hibernate.c3p0.timeout">50000</property>
    <!-- 最大的PreparedStatement的数量,不了解,网上都说设置为0不用去管理 -->
    <property name="hibernate.c3p0.max_statements">0</property>   
    <!-- 每隔3000秒检查连接池里的空闲连接 ,单位是秒-->
    <property name="hibernate.c3p0.idle_test_period">3000</property> 
    <!-- 当连接池耗尽并接到获得连接的请求,则新增加连接的数量 --> 
    <property name="hibernate.c3p0.acquire_increment">2</property> 
     <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="format_sql">true</property>
   <property name="show_sql">true</property>
   <property name="current_session_context_class">thread</property>
   </session-factory>
 
 </hibernate-configuration>

 以上配置不够用,还配置了hibernate.peoperties,我只是喜欢properties文件,其实两个文件可以合并

hibernate.c3p0.acquireRetryAttempts = 30
hibernate.c3p0.acquireRetryDelay = 1000
#checkin 和checkout出于性能考虑,配置为false,或默认
#hibernate.c3p0.testConnectionOnCheckin = false
hibernate.c3p0.automaticTestTable = c3p0TestTable
#将会被忽略,因为已加载hibernate.cfg.xml中的  hibernate.c3p0.idle_test_period
hibernate.c3p0.idleConnectionTestPeriod = 18000
hibernate.c3p0.checkoutTimeout=3000

建议大家看看hibernate源代码中的connection文件下面的7个类,然后去做相应的测试,来达到真正的了解。
以上文档若有错误,请及时留言指出,我会立即更改,希望不要误导初学者。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值