title: Should session and factory be closed?
body: <p>I'm using Hibernate 5.0.2.Final with a Data-Source connection (On a Tomcat 8.0.15) and started to ask myself if it's necessary to not only close the Session but also the SessionFactory?</p>
  
  <p>Right now it looks like this:</p>
  
  <pre><code>public static List&lt;HibernateList&gt; getHibernateList() {
          Session session = null;
          final String hql = "SELECT H FROM myhibernate.MyHibernate";
          try {
              SessionFactory factory = HibernateUtil.getSessionFactory();
              session = factory.openSession();
              session.beginTransaction();
  
              Query query = session.createQuery(hql);
  
              return query.list();
          } catch (HibernateException hibex) {
              Logger.getLogger(Hibernatepicker.class.getName()).log(Level.INFO, null, hql);
              Logger.getLogger(Hibernatepicker.class.getName()).log(Level.SEVERE, null, hibex);
          } finally {
              try {
                  if (session != null) {
                      session.close();
                  }
              } catch (HibernateException hibex) {
              }//Nothing I could do...
          }
          return null;
      }
  </code></pre>
  
  <p>Some details from the hibernate.cfg.xml</p>
  
  <pre><code>&lt;property name="hibernate.connection.datasource"&gt;java:comp/env/jdbc/sqlserv&lt;/property&gt;        
  &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt; 
  &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt; 
  &lt;property name="hbm2ddl.auto"&gt;auto&lt;/property&gt; 
  &lt;property name="show_sql"&gt;false&lt;/property&gt;       
  &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt;  
  </code></pre>
  
  <p>And the HibernateUtil:</p>
  
  <pre><code>public class HibernateUtil {
  private static final SessionFactory sessionFactory;
  
  static {
      try {
          Configuration cfg = new Configuration();
          sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
      } catch (Throwable ex) {
          Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
          throw new ExceptionInInitializerError(ex);
      }
  }
  
  public static SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  }
  </code></pre>
  
  <p>I'm undecided whether it's necessary or not to call this method in the finally-block instead of only closing the session:</p>
  
  <pre><code>public static void disconnect(Session session, SessionFactory factory) {
          try {
              if (session != null) {
                  session.close();
              } else {
                  Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Session is Null");
          }
  
      } catch (HibernateException | NullPointerException hibex) {
          Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
          Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
      }
      try {
          if (factory != null) {
              factory.close();
          } else {
              Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Factory is Null");
          }
  
      } catch (HibernateException | NullPointerException hibex) {
          Logger.getLogger(HibernateUtil.class.getName()).log(Level.INFO, null, "Couldn't close session, but there's nothing we can do...");
          Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, hibex);
      }
  }
  </code></pre>