Hibernate4下配置c3p0
maven中只需要为Hibernate4 配置hibernate-c3p0构件即可使用c3p0连接池了,它会自动传递添加c3p0的依赖。需要注意的是provider_class已经改为了:
<property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
默认配置,即不配置参数的情况下,其c3p0的配置值为:
初始化参数: INFO[main](AbstractPoolBackedDataSource.java:462)- Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c0f13ed [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@6400730a [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1d38u1gctiu5mjn7j|117015d, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 150, maxStatementsPerConnection -> 0, minPoolSize -> 3, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@5b919405 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gctiu5mjn7j|16566fb, jdbcUrl -> jdbc:h2:tcp://localhost/~/dbname, properties -> {useUnicode=true, user=******, password=******, characterEncoding=UTF-8} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gctiu5mjn7j|1ceb7ea, numHelperThreads -> 3 ]
指定官方文档上的6个参数
<property name="c3p0.min_size">2</property> <property name="c3p0.max_size">4</property> <property name="c3p0.timeout">122</property><!--对应maxIdleTime--> <property name="c3p0.max_statements">121</property> <property name="c3p0.acquire_increment">101</property> <property name="c3p0.idle_test_period">301</property>
结果为:
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@cb8c9ecc [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@78208b2a [ acquireIncrement -> 101, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1d38u1gcy17mypv41a|ff1e85, idleConnectionTestPeriod -> 301, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 122, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 4, maxStatements -> 121, maxStatementsPerConnection -> 0, minPoolSize -> 2, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@83de9879 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gcy17mypv41a|1ba8574, jdbcUrl -> jdbc:h2:tcp://localhost/~/dbname, properties -> {useUnicode=true, user=******, password=******, characterEncoding=UTF-8} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gcy17mypv41a|18a1cf1, numHelperThreads -> 3 ]
再添加参数:
<property name="c3p0.preferredTestQuery">select now();</property>
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@f9ee2eb0 [
connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@2a3dbc27
[
****
preferredTestQuery -> select now();, 已经出现了
*****
]
很好,因为c3p0官网上也一直提供6个参数的配置,而hibernate的说明文档中也仅提到了6个参数。
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.testConnectionsOnCheckout hibernate.c3p0.validate hibernate 2.x only!
Hibernate 中 c3p0文档
org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
Important configuration properties for the c3p0 connection pool
hibernate.c3p0.min_size
hibernate.c3p0.max_size
hibernate.c3p0.timeout
hibernate.c3p0.max_statements
而且,在Hibernate Environment类中也仅为这6个量定义了常量,一直认为c3p0的其它参数都不可用了呢。
现在再看下hibernate-c3p0-4.2.1.Final.jar源码(也只有二个类)C3P0ConnectionProvider的public void configure(Map props)方法中:
199~203行
Map allProps = new HashMap(); allProps.putAll( props ); allProps.putAll( c3props ); //c3props 会覆盖props中的部分配置 ds = DataSources.pooledDataSource( unpooled, allProps );
//创建allProps,并将props,c3props放入并通过c3p0的DataSources当初始化参数传入,从而初始化连接池ds.
在165~171行中对props处理
if ( key.startsWith( "hibernate.c3p0." ) ) { String newKey = key.substring( 15 ); if ( props.containsKey( newKey ) ) {//6个配置项 warnPropertyConflict( key, newKey ); } c3props.put( newKey, props.get( key ) ); //可以看到只要是hibernate.c3p0.开头的配置项,最终都会保存到c3props中去的。 }
添加参数测试
<property name="hibernate.c3p0.automaticTestTable">test_db</property> <property name="hibernate.c3p0.checkoutTimeout">60</property> <property name="hibernate.c3p0.idleConnectionTestPeriod">32</property> <property name="hibernate.c3p0.initialPoolSize">1</property> <property name="hibernate.c3p0.maxConnectionAge">601</property> <property name="hibernate.c3p0.maxIdleTime">62</property> <property name="hibernate.c3p0.minPoolSize">1</property> <property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
值都体现出来了
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@8a086ace [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@c3d8d643 [ acquireIncrement -> 101, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> test_db, breakAfterAcquireFailure -> false, checkoutTimeout -> 60, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1d38u1gdx4xz15w5nek|5385d7, idleConnectionTestPeriod -> 301, initialPoolSize -> 1, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 601, maxIdleTime -> 122, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 4, maxStatements -> 121, maxStatementsPerConnection -> 0, minPoolSize -> 2, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@c72ec116 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gdx4xz15w5nek|33be80, jdbcUrl -> jdbc:h2:tcp://localhost/~/dbname, properties -> {useUnicode=true, user=******, password=******, characterEncoding=UTF-8} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> true, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge1d38u1gdx4xz15w5nek|12f3c7a, numHelperThreads -> 3 ]
相关推荐
hibernate 配置 C3P0 连接池
hibernate3.3版本后,hibernate配置c3p0需要的jar包,如果没有这个包,可能会报异常,找不到provider-class的那个类
c3p0Jar包,Hibernate配置数据源必备,包含xml配置文件
Hibernate 下配置连接池解决多连接的问题
(hibernate调用C3p0)hibernate.cfg.xml
hibernate c3p0实例源码,配置hibernate使用c3p0连接池测试demo
NULL 博文链接:https://12345678.iteye.com/blog/718264
hibernate配置c3p0数据连接池 hibernate配置c3p0数据连接池 hibernate配置c3p0数据连接池
c3p0-0.9.2.1.jar&&hibernate-c3p0-4.0.0.Final[1].jar&&mchange-commons-java-0.2.3.4.jar
内容概要:c3p0连接池需要jar包.rar以及相关配置文件 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate、Spring等。 适合人群:正在学习...
c3p0 hibernat缓存 详细配置
Hibernate框架,默认推荐使用C3P0作为连接池实现,一个开源的连接池。 拷贝c3p0的jar到lib目录 (c3p0-0.9.5.2.jar , mchange-commons-java-0.2.12.jar) 把配置文件c3p0.xmla拷贝到src目录下(配置文件名一定不能修改...
一个hibernate数据源的c3p0配置,希望对你有帮助
博文链接:https://wxinpeng.iteye.com/blog/203088
c3p0详细配置描了属性的作用使用方法,怎样和hibernate使用
将两个包考入lib目录即可使用 包括c3p0-0.9.1.jar和hibernate-c3p0-4.1.11.Final.jar
c3p0在spring中的配置及所需jar包,一般用hibernate时用c3p0比较稳定一些。
c3p0连接池配置, 个人找了很多资料 都在.txt里面,有项目完整配置,解释,还有一些相关网站资料等
DB与我的应用程序相连,使用的是Hibernate的C3P0连接DB,突然重启DB,出现乘用程序报错,而且再进行注入,注入失败
hibernate连接池详解:c3p0 dbcp等三种配置连接池的方式