我正在做一個RESTful服務與數據庫進行通信,使用Hibernate作爲ORM。克服休眠5連接限制
我面臨的問題是Hibernate的連接池限制,只要達到極限就會拋出異常。
Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
1)我已經嘗試設置最大池大小在hibernate.cfg.xml
<property name="connection.pool_size">10</property>
2)我已經試過,而不是打開一個新的Session每次獲取當前連接
public static Session getCurrentSession(){
try{
return sessionFactory.getCurrentSession();
}
catch(Exception e){
try {
return sessionFactory.openSession();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
我總是最終達到limit
。
有沒有辦法完全克服這一點?
通常,這表示會話泄漏,從而導致連接泄漏。確保您打開的每個會話都關閉。否則它保持打開狀態,並且保持與打開/正在使用的數據庫的連接。 –