The last packet successfully received from the server was 51,898,257 milliseconds ago. The last packet sent successfully to the server was 47 milliseconds ago.
10:22:35,453 ERROR LoginDAOImpl:150 - LoginDAOImpl - getLogin()-org.hibernate.exception.JDBCConnectionException: could not execute query
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed by the driver.
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
Getting the session using hibernate :
public Session getSession() {
Long sessionFactoryCreationTime = (Long) ServletActionContext.getServletContext().getAttribute(HibernateListener.SEESSION_FACTORY_CREATION_TIME);
long currentMilliSeconds = System.currentTimeMillis();
long milliSeconds = currentMilliSeconds - sessionFactoryCreationTime.longValue();
int hours = (int) ((milliSeconds / (1000*60*60)) % 24);
if (hours > 6) {
reCreateSessionFactory();
}
Session session = null;
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
session = sessionFactory.openSession();
return session;
}
public void reCreateSessionFactory() {
Configuration config;
SessionFactory factory;
try {
String urlPath =(String) ServletActionContext.getServletContext().getAttribute(HibernateListener.CFG_PATH);
URL url = HibernateListener.class.getResource(urlPath);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
ServletActionContext.getServletContext().setAttribute(HibernateListener.KEY_NAME, factory);
ServletActionContext.getServletContext().setAttribute(HibernateListener.SEESSION_FACTORY_CREATION_TIME, Long.valueOf(System.currentTimeMillis()));
} catch (Exception e) {
log.error("AbstractDaoImpl::reCreateConnection Exception"+e.getMessage());
}
log.info("AbstractDaoImpl::reCreateSessionFactory==>Exit");
}
HibernateListener.java:
public class HibernateListener implements ServletContextListener {
private Logger log =LoggerFactory.getLogger(HibernateListener.class);
private String path = "hibernate.cfg.xml";
private static Class clazz = HibernateListener.class;
public static final String KEY_NAME = clazz.getName();
public static final String SEESSION_FACTORY_CREATION_TIME = "SEESSION_FACTORY_CREATION_TIME";
public static final String CFG_PATH = "CFG_PATH";
public void contextDestroyed(ServletContextEvent event) {
//
}
/**
*
* This method initializes Hibernate Session.
* @param event
*
*/
public void contextInitialized(ServletContextEvent event) {
log.info("HibernateListener::contextInitialized==>Entry");
Configuration config;
SessionFactory factory;
try {
URL url = HibernateListener.class.getResource(path);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
//save the Hibernate session factory into serlvet context
event.getServletContext().setAttribute(KEY_NAME, factory);
event.getServletContext().setAttribute(CFG_PATH, path);
event.getServletContext().setAttribute(SEESSION_FACTORY_CREATION_TIME, Long.valueOf(System.currentTimeMillis()));
} catch (Exception e) {
log.error("HibernateListener::contextInitialized Exception"+e.getMessage());
}
log.info("HibernateListener::contextInitialized==>Exit");
}
}
Need to add more solutions...