title: EntityManagerFactory is closed when page is reloaded
body: <p>I use this Java code to make SQL queries using Hibernate in JSF application and SQLite database:</p>
  
  <p><strong>Hibernate configuration file:</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.connection.datasource"&gt;java:comp/env/jdbc/sqliteDB&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.SQLiteDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;org.sqlite.JDBC&lt;/property&gt;
          &lt;property name="hibernate.connection.release_mode"&gt;auto&lt;/property&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
          &lt;property name="hibernate.connection.autoReconnect"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
          &lt;mapping class="com.web.models.SystemUsers"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>Factory</strong>:</p>
  
  <pre><code> public class HibernateUtil
      {
          private static final SessionFactory sessionFactory;
          private static ServiceRegistry serviceRegistry;
  
          static
          {
              try
              {
                  StandardServiceRegistry standardRegistry
                      = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
                  Metadata metaData
                      = new MetadataSources(standardRegistry).getMetadataBuilder().build();
                  sessionFactory = metaData.getSessionFactoryBuilder().build();
              }
              catch (Throwable th)
              {
                  System.err.println("Enitial SessionFactory creation failed" + th);
                  throw new ExceptionInInitializerError(th);
              }
          }
  
          public static SessionFactory getSessionFactory()
          {
              return sessionFactory;
          }
      }
  </code></pre>
  
  <p><strong>I use this code to call Hibernate factory class:</strong></p>
  
  <pre><code>private List&lt;SystemUserObj&gt; generateSystemUsers()
      {
          try (SessionFactory sessFact = HibernateUtil.getSessionFactory())
          {
              Session session = sessFact.getCurrentSession();
              Transaction tr = session.beginTransaction();
  
              List&lt;SystemUsers&gt; dataList = session.createQuery("from SystemUsers").list();
  
              for (SystemUsers user : dataList)
              {
                  ........
              }
  
              session.flush();
              tr.commit();
          }
  
          return list;
      }
  </code></pre>
  
  <p>Then I open the web page first time data is loaded properly but when I open the page page second time I get error:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: EntityManagerFactory is closed
  </code></pre>
  
  <p>Do you know how I can fix this issue?
  Probably my factory class is not properly implemented?</p>
  
  <p>Also how I can refactor the Factory code so I can made several requests simultaneously?</p>