title: Hibernate 3.2 and apache dbcp eats postgres connections
body: <p>I have a simple entity bean and use hibernate 3.2.5 to persist it to postgres, prod will be pg 8.4, while I currently develop on 9.1. I use apache commons dbcp 1.4 (and commons pool 1.5.4) a public class DBCPConnectionProvider implements ConnectionProvider as googled from the web <a href="http://wiki.apache.org/commons/DBCP/Hibernate" rel="nofollow">http://wiki.apache.org/commons/DBCP/Hibernate</a></p>
  
  <p>Earlier I used c3po and mysql instead of dbcp. Now I get </p>
  
  <p>Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection</p>
  
  <p>and </p>
  
  <p>Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections</p>
  
  <p>Using mysql/c3po earlier, I got similar errors.</p>
  
  <p>Currently persistence.xml properties looks like this:</p>
  
  <pre><code>  &lt;property name="hibernate.connection.username" value="marin-legacy"/&gt;
    &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/&gt;
    &lt;property name="hibernate.connection.password" value="marin-legacy"/&gt;
    &lt;property name="hibernate.connection.url" value="jdbc:postgresql://localhost/database"/&gt;
    &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/&gt;
    &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
    &lt;property name="show_sql" value="true"/&gt;
  
    &lt;property name="hibernate.connection.provider_class" value="vinter2010.persistence.DBCPConnectionProvider"/&gt;
    &lt;property name="hibernate.dbcp.initialSize" value="1"/&gt;
    &lt;property name="hibernate.dbcp.maxActive" value="5"/&gt;
    &lt;property name="hibernate.dbcp.maxIdle" value="5"/&gt;
    &lt;property name="hibernate.dbcp.minIdle" value="1"/&gt;
  </code></pre>
  
  <p>Using this I would expect dbcp to keep 5 connections open and reassigning those to javacode.</p>
  
  <p>Currently I have a servlet that inserts records. It succeeds with a number of insert more or less equal to the number of max_connections in postgresql.conf, which is 100. After that it fails with the above errors. Currently the servlet is called by a client that runs in loop apx 350 times, posting one row to store for the servlet in each loop.</p>
  
  <p>If I run without dbcp or other pool, I get the same exception, only change is that it is thrown by hibernate and not dbcp.</p>
  
  <p>I would have expected dbcp to just allocate 5 connections, and hibernate to open and close just one connection at a time.</p>
  
  <p>The actual code is very simple, and basically generated by the jpa-generator in netbeans 6.5.</p>
  
  <p>This is the create-function that I call from my servlet (code by Netbeans):</p>
  
  <pre><code>    public void create(WebOrderWinter2 webOrderWinter2) {
      EntityManager em = null;
      try {
          em = getEntityManager();
          em.getTransaction().begin();
          em.persist(webOrderWinter2);
          em.getTransaction().commit();
      } finally {
          if (em != null) {
              em.close();
          }
      }
  }
  </code></pre>
  
  <p>The entity bean is equally standard, basically like Netbeans generated it, and it works flawlessly, can't imagine the connection problem lies there. It seems I have some code/configuration that makes Hibernate overrule the connection pool configuration and have the pool open new connections instead of reusing old ones. Anone have any clues here?</p>