`
ponlya
  • 浏览: 159582 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

HIbernate 配置C3p0

 
阅读更多

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 
 ]

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics