我正在使用Tomcat和MySQL创建Java EE应用程序。在hibernate.cfg.xml文件中,我将连接池大小设置为10
<property name="connection.pool_size">10</property>
我还创建了一个DAO:
public Rules getRulesById(int id) {
Rules rules;
factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
try {
Query query = session.createQuery("SELECT a FROM Rules a WHERE a.id=:id");
query.setParameter("id", id);
rules = (Rules) query.getSingleResult();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
} finally {
session.close();
}
return rules;
}
查询数据库大约30次后,出现错误:
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/GoTown] threw exception [org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]] with root cause
java.sql.SQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
我不确定为什么,我要关闭会话:session.close();
谢谢!