title: c3p0 creating more connections than specified in configuration
body: <p>My application is using Hibernate 4.1.7 and c3p0 0.9.1.</p>
  
  <p>I've set the c3p0.max_size property in my hibernate.cfg.xml file for my application to 50, but the number of JDBC connections created has been exceeding that value.  Also, inactive/idle connections are not being removed as I've also specified in my Hibernate configuration.  Here's a snip from my configuration: </p>
  
  <pre><code>&lt;property name="c3p0.acquire_increment"&gt;1&lt;/property&gt;
  &lt;property name="c3p0.autoCommitOnClose"&gt;false&lt;/property&gt;
  &lt;property name="c3p0.max_size"&gt;50&lt;/property&gt;
  &lt;property name="c3p0.min_size"&gt;1&lt;/property&gt;
  &lt;property name="c3p0.numHelperThreads"&gt;1&lt;/property&gt;
  &lt;property name="c3p0.maxIdleTime"&gt;30&lt;/property&gt; 
  &lt;property name="c3p0.maxIdleTimeExcessConnections"&gt;20&lt;/property&gt;
  &lt;property name="c3p0.maxConnectionAge"&gt;45&lt;/property&gt;
  </code></pre>
  
  <p>I am explicitly closing my sessions and session factories in a finally block within my code.  Here's the class that I'm using to create my SessionFactory instance: </p>
  
  <pre><code>package ics.sis.util;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  
  import ics.global.runtime.Environment;
  import ics.util.properties.PropertiesISUWrapper;
  
  public class HibernateSessionFactory {
      private static SessionFactory sessionFactory;
      private static ServiceRegistry serviceRegistry;
      private static final PropertiesISUWrapper ISU_PROPERTIES = new PropertiesISUWrapper(Environment.getName(),"VzAppIntegration"); 
  
      public static SessionFactory create() {
          Configuration configuration = new Configuration();
          configuration.configure();
  
          configuration.setProperty("hibernate.connection.url", ISU_PROPERTIES.getUrl());
          configuration.setProperty("hibernate.connection.password", ISU_PROPERTIES.getPassword());
  
          serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
          sessionFactory = configuration.buildSessionFactory(serviceRegistry);
          return sessionFactory;
      }
  
  }
  </code></pre>
  
  <p>Here's one of the main methods performing a database transaction: </p>
  
  <pre><code>public static int insert(int aidm, String termCode, String wappCode) throws SQLException, ClassNotFoundException {      
          // Initialize session and transaction
          SessionFactory sessionFactory = HibernateSessionFactory.create();
          Session session = sessionFactory.openSession();
          Transaction tx = null;
          int applSeqno = 0;
          Stvwapp wapp = null;
  
          try {
              tx = session.beginTransaction();
  
              applSeqno = generateApplSeqNo(session, aidm);
              SarheadId sarheadIdDao = new SarheadId();
              sarheadIdDao.setSarheadAidm(aidm);
              sarheadIdDao.setSarheadApplSeqno((short)applSeqno);
  
              // Find STVWAPP row by WAPP code
              Query query = session.getNamedQuery("findStvwappByWappCode"); 
              query.setString("wappCode", wappCode);
  
              if (query.list().size() == 0) {
                  throw new RuntimeException("Invalid WAPP code specified: " + wappCode);
              } else {
                  wapp = (Stvwapp) query.list().get(0);
              }
  
              Sarhead sarheadDao = new Sarhead();
              sarheadDao.setId(sarheadIdDao);
              sarheadDao.setSarheadActivityDate(new java.sql.Timestamp(System.currentTimeMillis()));
              sarheadDao.setSarheadAddDate(new java.sql.Timestamp(System.currentTimeMillis()));
              sarheadDao.setSarheadAplsCode("WEB");
              sarheadDao.setSarheadApplAcceptInd("N");
              sarheadDao.setSarheadApplCompInd("N");
              sarheadDao.setSarheadApplStatusInd("N");
              sarheadDao.setSarheadPersStatusInd("N");
              sarheadDao.setSarheadProcessInd("N");
              sarheadDao.setSarheadTermCodeEntry(termCode);
              sarheadDao.setStvwapp(wapp);
  
              session.save(sarheadDao);
          } finally {
              tx.commit();
              session.close();
              sessionFactory.close(); 
          }
  
          return applSeqno;
      }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I changed the log level of c3p0 to DEBUG to get more verbose logging on connection pooling and I'm seeing it check every 3 or 4 seconds for expired connections.  Also, I'm seeing the following line being logged, which to me looks like there are two connections total in the pool.  However, in Toad, I'm monitoring the total JDBC connections open and it shows 6.  So I'm trying to figure out why there's a discrepancy between these numbers.</p>
  
  <blockquote>
    <p>[Env:DEVL] [] - 2012-12-06 12:14:07  DEBUG BasicResourcePool:1644 -
    trace com.mchange.v2.resourcepool.BasicResourcePool@7f1d11b9 [managed:
    1, unused: 1, excluded: 0] (e.g.
    com.mchange.v2.c3p0.impl.NewPooledConnection@7b3f1264)</p>
  </blockquote>