我正在做一个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
。
有没有办法完全克服这一点?
通常,这表示会话泄漏,从而导致连接泄漏。确保您打开的每个会话都关闭。否则它保持打开状态,并且保持与打开/正在使用的数据库的连接。 –