我的應用程序使用Hibernate 4.1.7和c3p0 0.9.1。c3p0創建比配置中指定的更多連接
我已將我的應用程序的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月6日12時14分07秒DEBUG BasicResourcePool:1644 - 跟蹤[email protected] [管理: 1,未使用的: 1,排除:0](如 [email protected])
這就是我在我的答案中所描述的......:P –