title: Hibernate Session Factory
body: <p>In our web application we have a <code>HibernateSessionFactory</code> class, that is opening and closing connections. Everything is okay, but when we are updating data in the database, it doesn't change in our application. Unfortunately, we see old data from the database. How can I fix it?</p>
  
  <pre><code>public class HibernateSessionFactory {
  
      private static final ThreadLocal threadLocal = new ThreadLocal();
      private static org.hibernate.SessionFactory sessionFactory;
  
      private static Configuration configuration = new Configuration();
      private static ServiceRegistry serviceRegistry; 
  
      private static final Logger log = Logger.getLogger(HibernateSessionFactory.class);
  
      static {
          try {
              configuration.configure();
              serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
              . buildServiceRegistry();
              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
          } catch (Exception e) {
              log.error("Error Creating SessionFactory",e);
  
          }
      }
  
      private HibernateSessionFactory() {
      }
  
      public static Session getSession() throws HibernateException {
          Session session = (Session) threadLocal.get(); 
          if (session == null || !session.isOpen()) {
              if (sessionFactory == null) {
                  rebuildSessionFactory();
              }
              session = (sessionFactory != null) ? sessionFactory.openSession()
              : null;
  
              threadLocal.set(session);
          }
          return session;
      }
  
      public static void rebuildSessionFactory() {
  
          try {
              configuration.configure();
              serviceRegistry = new ServiceRegistryBuilder(). applySettings(configuration.getProperties()) 
              .buildServiceRegistry();
              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
          } catch (Exception e) {
              log.error("Error Creating SessionFactory",e);
          }
      }
  
      public static void closeSession() throws HibernateException {
          Session session = (Session) threadLocal.get();
          threadLocal.set(null);
          if (session != null) {
              session.flush();
              session.close();
  
          }
      }
  
      public static org.hibernate.SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static Configuration getConfiguration() {
          return configuration;
      }
  
  }
  </code></pre>