title: Hibernate : closing the session factory does not close the c3p0 connection pool
body: <p>I recently started using hibernate along with c3p0 as the ORM in my application. However, when I close the session factory, the connection pool does not close itself! This is <em>the one and only place</em> in my application where I do anything with a session.</p>
  
  <pre><code>    StatelessSession session = null;
      Transaction transaction = null;
  
  
      try {
          session = sessionFactory.openStatelessSession();
          transaction = session.beginTransaction();
  
          List&lt;Thingy&gt; list = session.getNamedQuery("getAvailableThingy").list();
  
          transaction.commit();
          return list;
  
      } catch (Exception error) { 
          if (transaction != null) {
              transaction.rollback();
          }
          throw error;
      } finally {
          if (session != null) {
              session.close();
          }
      }
  </code></pre>
  
  <p>This is my <code>hibernate.cfg.xml</code> configuration file </p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
          &lt;property name="javax.persistence.validation.mode"&gt;none&lt;/property&gt;
          &lt;property name="hibernate.connection.release_mode"&gt;after_transaction&lt;/property&gt;
  
          &lt;property name="hibernate.c3p0.minPoolSize"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.c3p0.maxPoolSize"&gt;2&lt;/property&gt;
          &lt;property name="hibernate.c3p0.acquireIncrement"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.c3p0.initialPoolSize"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.c3p0.timeout"&gt;30&lt;/property&gt;
          &lt;property name="hibernate.c3p0.maxIdleTimeExcessConnections"&gt;5&lt;/property&gt;
          &lt;property name="hibernate.c3p0.idleConnectionTestPeriod"&gt;300&lt;/property&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Note that the reason for the very short idle connection it that its the only way I found yet to make my integration tests to pass. They open and close the session factory a lot and thus I always run out of connections. As we are at the beginning of the project, I guess it's not a very sustainable strategy in the long run.</p>
  
  <p>An "interesting" thing to note is that despite the fact that I set the initial connection pool to one, c3p0 still try to open two connection on start. My guess is that there is some kind of hidden session somewhere that don't get closed (but where? beat me).</p>
  
  <p>So how can I get that annoying connection pool to close itself up?</p>
  
  <p>Additional info : 
  how I create and destroy my session factory</p>
  
  <pre><code>import static com.google.common.base.Preconditions.*;
  
  import javax.inject.Inject;
  import javax.inject.Provider;
  import javax.inject.Singleton;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.google.inject.Provides;
  
  
  @Singleton 
  public class PostgisConnection implements Provider&lt;SessionFactory&gt;, AutoCloseable {
      private final Logger logger = LoggerFactory.getLogger(getClass());
      private final ConnectionInfo connectionInfo;
      private SessionFactory sessionFactory = null;
  
      @Inject 
      public PostgisConnection(ConnectionInfo connectionInfo) {
          this.connectionInfo = connectionInfo;
      }
  
      public AutoCloseable open() {
          checkState(sessionFactory == null, "Connections to postgis are already open");
  
          logger.info("Creating sessionFactory for connection to postgis: {}", connectionInfo.getJdbcUrl());
          sessionFactory = newPostgisSessionFactory(connectionInfo);
  
          return this;
      }
  
      @Override
      public void close() throws Exception {
          try {
              if (sessionFactory != null) {
                  logger.info("Closing sessionFactory for postgis: {}", connectionInfo.getJdbcUrl());
                  sessionFactory.close();
                  checkState(sessionFactory.isClosed(), "Session factory should be closed at this point");
              }
          } catch (Exception error) {
              logger.error("Error closing SessionFactory", error);
          }
      }
  
      @Provides 
      public SessionFactory get() {
          return sessionFactory;
      }
  
      public static SessionFactory newPostgisSessionFactory(ConnectionInfo connectionInfo) {
          Configuration configuration = configurationWith(connectionInfo);
          return configuration.buildSessionFactory(registryFrom(configuration));
      }
  
      private static Configuration configurationWith(ConnectionInfo connectionInfo) {
          Configuration configuration = new Configuration();
          setConnectionInfo(connectionInfo, configuration);
          configuration.addURL(PostgisConnection.class.getResource("mapping.hbm.xml"));
          configuration.configure(PostgisConnection.class.getResource("hibernate.cfg.xml"));
  
          return configuration;
      }
  
      private static void setConnectionInfo(ConnectionInfo connectionInfo, Configuration configuration) {
          configuration.setProperty("hibernate.connection.url", connectionInfo.getJdbcUrl());
          configuration.setProperty("hibernate.connection.username", connectionInfo.getUsername());
          configuration.setProperty("hibernate.connection.password", connectionInfo.getPassword());
      }
  
      private static ServiceRegistry registryFrom(Configuration configuration) {
          return new ServiceRegistryBuilder()
                  .applySettings(configuration.getProperties())
                  .buildServiceRegistry();
      }
  
  }
  </code></pre>
  
  <ul>
  <li>Hibernate version : 4.1.10.Final</li>
  <li>C3p0 version : 0.9.1.2</li>
  </ul>