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连接池之前,我们先简要了解下Hibernate与C3P0的基本概念。 Hibernate是一个开放源代码的对象关系映射(ORM)框架,它实现了Java对象与数据库表之间的映射,允许开发人员以面向...
2. **Hibernate配置C3P0** 要在Hibernate中配置C3P0,首先需要在项目的类路径下添加C3P0的依赖库。常见的依赖管理工具有Maven或Gradle,对应的坐标如下: Maven: ```xml <groupId>com.mchange</groupId> ...
在使用Hibernate配置c3p0数据源时,我们需要引入`c3p0-jar`包,这个压缩包中的JAR文件包含了c3p0的所有相关类和库。在项目中,我们通常将这个JAR文件添加到类路径(ClassPath)中,以便于程序运行时能够找到并加载c3...
配置C3P0连接池主要涉及到以下步骤: **2.1 配置文件** C3P0的配置通常有两种方式,通过`hibernate.properties`或`hibernate.cfg.xml`文件。在`hibernate.properties`中,你需要定义一系列以`hibernate.c3p0.`开头...
在 Hibernate 中,C3P0 的配置主要通过 `hibernate.properties` 或 `hibernate.cfg.xml` 文件中的属性进行设置。这些配置项对于控制连接池的行为至关重要。 #### 3. 关键配置参数详解 ##### 3.1 hibernate....
hibernate3.3版本后,hibernate配置c3p0需要的jar包,如果没有这个包,可能会报异常,找不到provider-class的那个类
5. 配置C3P0:在Hibernate的配置文件(通常是hibernate.cfg.xml)中,需要添加C3P0的配置信息,如最小连接数、最大连接数、获取连接的超时时间、空闲测试频率等。例如: ```xml <property name="hibernate.c3p0.min_...
5. **配置c3p0**: 在使用Hibernate和c3p0时,需要在Hibernate的配置文件(如`hibernate.cfg.xml`)中指定c3p0的相关属性,如最小连接数、最大连接数、获取连接超时时间等,以满足特定应用的需求。 6. **集成步骤**:...
2. **配置c3p0** 在`Hibernate3.0`中使用`c3p0`,需要在`hibernate.cfg.xml`配置文件中添加相关的配置项。这些配置包括数据源的初始化参数,如最小连接数、最大连接数、测试连接的SQL语句等。例如: ```xml ...
### Hibernate + c3p0 连接池与 SQL Server 2000 的配置与问题解决 #### 一、背景介绍 在Java开发环境中,Hibernate作为一款流行的ORM框架,能够提供一套强大的对象-关系映射机制,使得开发者可以更加便捷地进行...
在本项目"SSH - SpringMVC4 + Spring4 + Hibernate4 + c3p0 + Mysql.zip"中,开发者使用了SpringMVC4作为表现层,Spring4作为控制层和服务层,Hibernate4作为持久层,c3p0作为数据库连接池,以及MySQL作为数据库。...
3. **异常处理**:对于连接不可用等情况,可以通过配置C3P0的异常处理策略来进行重试或自动恢复连接。 综上所述,正确配置Hibernate与C3P0的集成,不仅可以有效提高数据库访问效率,还能显著提升系统的稳定性和可...
在Hibernate的配置文件`hibernate.cfg.xml`中,我们需要添加C3P0的连接池配置。以下是一些常见的配置项: 1. `c3p0.min_size`:最小连接池大小,定义了连接池初始化时的最小连接数量。 2. `c3p0.max_size`:最大...
3. **配置C3P0**:详述如何在Hibernate的配置文件(hibernate.cfg.xml)中添加C3P0连接池的配置,包括minPoolSize、maxPoolSize、acquireIncrement、idleTestPeriod等关键参数的含义和设置。 4. **连接池管理**:...
本篇文章将详细介绍如何配置c3p0数据源,以及其在Hibernate框架中的应用。 首先,我们需要了解c3p0的基本概念和特点。c3p0提供了自动测试连接、空闲连接检测、连接池大小动态调整等功能,能够帮助我们更高效地管理...
然后,我们需要在Hibernate的配置文件(通常是hibernate.cfg.xml)中配置C3P0连接池。在`<session-factory>`标签内添加以下内容: ```xml <property name="hibernate.c3p0.min_size">5 <property name="hibernate....
描述中提到“hibernate+c3p0匹配架包+hibernate.properties”,意味着我们需要配置相应的库文件以及Hibernate的属性配置文件来创建一个适合开发的环境。`hibernate.properties`是Hibernate的配置文件,通常包含...
使用 **hibernate-c3p0-4.2.4**,你需要在 Hibernate 配置文件(通常是 `hibernate.cfg.xml`)中指定 C3P0 数据源,并设置相应的配置属性,如以下示例: ```xml <property name="hibernate.connection.datasource">...
(hibernate调用C3p0)hibernate.cfg.xml
配置C3P0连接池时,开发者通常需要在配置文件(如Hibernate的`hibernate.cfg.xml`或Spring的`applicationContext.xml`)中指定以下参数: - `driver_class`: 数据库驱动类名,例如`com.mysql.jdbc.Driver`。 - `...