我的应用程序使用的是 Hibernate 4.1.7 和 c3p0 0.9.1。
我已将应用程序的 hibernate.cfg.xml 文件中的 c3p0.max_size 属性设置为 50,但创建的 JDBC 连接数已超过该值。此外,我在 Hibernate 配置中也指定了非活动/空闲连接不会被删除。这是我的配置中的一个片段:
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.autoCommitOnClose">false</property>
<property name="c3p0.max_size">50</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.numHelperThreads">1</property>
<property name="c3p0.maxIdleTime">30</property>
<property name="c3p0.maxIdleTimeExcessConnections">20</property>
<property name="c3p0.maxConnectionAge">45</property>
我在代码中的 finally 块中明确关闭了我的会话和会话工厂。这是我用来创建 SessionFactory 实例的类:
package ics.sis.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import ics.global.runtime.Environment;
import ics.util.properties.PropertiesISUWrapper;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static final PropertiesISUWrapper ISU_PROPERTIES = new PropertiesISUWrapper(Environment.getName(),"VzAppIntegration");
public static SessionFactory create() {
Configuration configuration = new Configuration();
configuration.configure();
configuration.setProperty("hibernate.connection.url", ISU_PROPERTIES.getUrl());
configuration.setProperty("hibernate.connection.password", ISU_PROPERTIES.getPassword());
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
这是执行数据库事务的主要方法之一:
public static int insert(int aidm, String termCode, String wappCode) throws SQLException, ClassNotFoundException {
// Initialize session and transaction
SessionFactory sessionFactory = HibernateSessionFactory.create();
Session session = sessionFactory.openSession();
Transaction tx = null;
int applSeqno = 0;
Stvwapp wapp = null;
try {
tx = session.beginTransaction();
applSeqno = generateApplSeqNo(session, aidm);
SarheadId sarheadIdDao = new SarheadId();
sarheadIdDao.setSarheadAidm(aidm);
sarheadIdDao.setSarheadApplSeqno((short)applSeqno);
// Find STVWAPP row by WAPP code
Query query = session.getNamedQuery("findStvwappByWappCode");
query.setString("wappCode", wappCode);
if (query.list().size() == 0) {
throw new RuntimeException("Invalid WAPP code specified: " + wappCode);
} else {
wapp = (Stvwapp) query.list().get(0);
}
Sarhead sarheadDao = new Sarhead();
sarheadDao.setId(sarheadIdDao);
sarheadDao.setSarheadActivityDate(new java.sql.Timestamp(System.currentTimeMillis()));
sarheadDao.setSarheadAddDate(new java.sql.Timestamp(System.currentTimeMillis()));
sarheadDao.setSarheadAplsCode("WEB");
sarheadDao.setSarheadApplAcceptInd("N");
sarheadDao.setSarheadApplCompInd("N");
sarheadDao.setSarheadApplStatusInd("N");
sarheadDao.setSarheadPersStatusInd("N");
sarheadDao.setSarheadProcessInd("N");
sarheadDao.setSarheadTermCodeEntry(termCode);
sarheadDao.setStvwapp(wapp);
session.save(sarheadDao);
} finally {
tx.commit();
session.close();
sessionFactory.close();
}
return applSeqno;
}
更新
我将 c3p0 的日志级别更改为 DEBUG 以获得更详细的连接池日志记录,并且我看到它每 3 或 4 秒检查一次过期连接。此外,我看到正在记录以下行,在我看来,池中总共有两个连接。但是,在 Toad 中,我正在监视打开的 JDBC 连接总数,它显示为 6。所以我试图弄清楚为什么这些数字之间存在差异。
[Env:DEVL] [] - 2012-12-06 12:14:07 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@7f1d11b9 [托管:1,未使用:1,排除:0](例如com.mchange.v2.c3p0.impl.NewPooledConnection@7b3f1264)