title: Close SessionFactory in Hibernate 4.3
body: <p>I'm upgrading my Hibernate to the latest version. With my old <code>HibernateUtil.java</code> I had no problems but when upgrading it, the SessionFactory doesn't seem to close anymore. </p>
  
  <p>This is my <strong>new</strong> <code>HibernateUtil.java</code> class:</p>
  
  <pre><code>import org.hibernate.HibernateException;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.SessionFactory;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory;
  
      static {
          try {
              Configuration configuration = new Configuration().configure();
              StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
              sessionFactory = configuration.buildSessionFactory(builder.build());
          } catch (HibernateException ex) {
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static void closeSessionFactory() {
          sessionFactory.close();
      }
  }
  </code></pre>
  
  <p>This is my <strong>old</strong> <code>HibernateUtil.java</code> class:</p>
  
  <pre><code>import org.hibernate.cfg.Configuration;
  import org.hibernate.SessionFactory;
  
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory;
  
      static {
          try {
              // Create the SessionFactory from standard (hibernate.cfg.xml) 
              // config file.
              sessionFactory = new Configuration().configure().buildSessionFactory();
          } catch (Throwable ex) {
              // Log the exception. 
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static void closeSessionFactory() {
          sessionFactory.close();
      }
  }
  </code></pre>
  
  <p>This is my <strong>hibernate.cfg.xml</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Database connection settings --&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/mydatabase&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;user&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;pass&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;false&lt;/property&gt;
          &lt;property name="format_sql"&gt;false&lt;/property&gt;
          &lt;property name="use_sql_comments"&gt;false&lt;/property&gt;
  
  
          &lt;!-- Use the thread as the context --&gt;
          &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Use these files for mapping configuration --&gt;
          &lt;mapping resource="test/Person.hbm.xml"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Code in which I create the session:</p>
  
  <pre><code>public class Helper {
  
      Session session = null;
  
      public Helper() {
          this.session = HibernateUtil.getSessionFactory().getCurrentSession();
      }
  
      public List getPeople(int id) {
         ...
      }
  }
  </code></pre>
  
  <p>Main method:</p>
  
  <pre><code>public static void main(String args[]) {
      Logger log = Logger.getLogger("org.hibernate");
      log.setLevel(Level.WARNING);
  
      Helper helper = new Helper();
      List&lt;Person&gt; people = helper.getPeople(1);
  
      for (int i = 0; i &lt; people.size(); i++) {
          System.out.println("people " + i + ": " + people.get(i).getID());
      }
  
      HibernateUtil.closeSessionFactory();
  }
  </code></pre>