我正在尝试为网站的休眠设置 c3p0 连接池。在 MS SQL 数据库中运行 select number of connections 我看到在 min_size 属性中指定的连接数量是双倍的。查看日志后,我发现连接池似乎被初始化了两次。有谁知道为什么会发生?
日志:
[INFO] c3p0.C3P0Registry Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42;
debug? true; trace: 10]
[INFO] impl.AbstractPoolBackedDataSource Initializing c3p0 pool...
com.mchange.v2.c3p0.PoolBackedDataSource@bb009603 [ connectionPoolDataSource ->
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@6dd5f639 [ acquireIncrement -> 3,
acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, .......
在这里几行之后,我们又来了:
[INFO] impl.AbstractPoolBackedDataSource Initializing c3p0 pool...
com.mchange.v2.c3p0.PoolBackedDataSource@fbb6bb58 [ connectionPoolDataSource ->
com.mchange.v2.c3p0.Wrap......
我的休眠属性(部分):
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.0.67:1433/SSS2;instanceName=SQLEXPRESS;</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">DEV</property>
<property name="hibernate.connection.password">123</property>
<property name="hbm2ddl.auto">validate</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">6</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<mapping class="***.entities.User" />
<mapping class="***.entities.UserRole" />
<mapping class="***.entities.AccountType" />
Maven POM(部分):
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<!-- Hibernate c3p0 connection pool -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate-c3p0-version}</version>
</dependency>
-------
<tapestry-release-version>5.3.3</tapestry-release-version>
<hibernate-c3p0-version>3.6.0.Final</hibernate-c3p0-version>
我在每个事务中使用会话工厂创建休眠会话,并在 try-catch 语句的 finally 块中关闭它。(HibernateFactory 只是在需要时构建工厂并打开会话)
protected <T> List <T> findAll(Class<T> clazz) {
List <T> objects = null;
Session session = HibernateFactory.openSession();
Transaction tx = session.beginTransaction();
try {
Query query = session.createQuery("from "+clazz.getName()+" as user left join fetch user.userRole");
objects = query.list();
tx.commit();
} catch (HibernateException e) {
handleException(e, tx);
} finally {
HibernateFactory.close(session);
}
return objects;
}
相关链接: