我收到太多连接错误。如果我增加max_connections数字它不会来,但它最终会来。我在每个方法中创建新会话但是也关闭它。 这是示例方法:
public List<ActivityLog> loadRecords(Integer stageId) {
Session session = createSession();
List<ActivityLog> resultsSelect = null;
try {
String hqlSelect = "FROM ActivityLog C WHERE stage_id=:stageId and status=:status";
Query querySelect = session.createQuery(hqlSelect);
querySelect.setParameter("stageId", stageId);
querySelect.setParameter("status", "Select");
resultsSelect = querySelect.list();
} finally {
session.close();
}
return resultsSelect;
}
这是我的连接工厂:
public static SessionFactory createSessionFactory(Configuration configuration) {
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static Session createSession() {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = createSessionFactory(cfg);
Session session = factory.openSession();
return session;
}
这是我得到的错误: enter image description here 请帮帮我。
错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:数据源拒绝建立连接,来自服务器的消息:“连接太多”