我正在尝试为网站设置hibernate的c3p0连接池。 在MS SQL数据库中运行选定数量的连接我看到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......
我的Hibernate属性(部分):
<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>
我使用会话工厂创建每个事务的hibernate会话,并在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;
}
相关链接:
答案 0 :(得分:0)
我认为&#34; hbm2ddl.auto&#34;参数强制双重初始化... 尝试从hibernate cfg文件中删除验证,tapestry应该只初始化一次会话