我将 Hibernate 4.3.0 与 MySQL 和 Tomcat 一起使用。所有必需的库都在类路径中,这里是 hibernate.cfg.xml
:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="current_session_context_class">thread</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
使用上述设置,在连接到数据库应用程序 20 次后不再连接,而且我没有在应用程序日志中找到与此行为相关的信息。
有谁知道哪里出了问题,我该如何正确设置 c3p0 和 hibernate ?
最佳答案
设置没问题。连接耗尽的原因是连接没有正确释放。
确保你:
- 成功提交交易
- 失败时回滚事务
- 完成后关闭 Hibernate session :
因此,如果您没有 Spring 代表您处理事务/ session 管理,那么这就是您应该如何操作 Hibernate session :
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
}finally {
session.close();
}
关于java - hibernate 所需的 C3P0 设置是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848743/