I am getting Too many connections error. If I increase max_connections number it doesn't come but it will eventually come.I am creating new session in each method but also closing it. This is sample method:
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;
}
This is my connection factory:
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;
}
Here is the error I am getting: enter image description here Please help me.
Error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"