Tuesday, January 18, 2011

Hibernate Connection Fails if there is Database inconsistency or DB fail-over (Oracle RAC)

        /**
     * A static method with returns Hibernate Sessoin Object.
     *
     * @return {@link Session}
     */
    public static Session getSession() throws HibernateException {
        Session appSession = sessionFactory.openSession();
        try
        {
                if(appSession.connection().isClosed()||!appSession.isConnected()||!appSession.isOpen()){
                        System.out.println("Session Closed, Creating Session again...");
                        appSession.reconnect(appSession.connection());
                }
        }
            catch (Exception e) {
                System.err.println("Got Problem while getting Session from Factory:"+ e.getMessage());
                e.printStackTrace();
            }
        return appSession;
      
    }