Tuesday 13 August 2013

Communications link failure:

Communication Link Failure:
While login into application, we are getting communication link failure with the below error message.


10:22:35,453 ERROR JDBCExceptionReporter:78 - Communications link failure
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
10:22:35,453 ERROR JDBCTransaction:168 - JDBC rollback failed
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed by the driver.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)




Solution 1: Implementing the Connection Pool mechanism
Solution 2: change default value in MySQL configuration file (connect-timeout option in mysqld section) -
[mysqld]
connect-timeout=100
Solution 4: Reconnect the session again after closing the session - For this need to right some code in your application.


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...

No comments:

Post a Comment

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...