title: JPA + Datanucleus + Connection pooling : connections are opened/closed with each statement
body: <p>I am using JPA and data nucleus, in order to access a postgresql database.
  My setup is as follows :</p>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;persistence-unit name="postgresql" &gt;
      &lt;provider&gt;org.datanucleus.api.jpa.PersistenceProviderImpl&lt;/provider&gt;
      &lt;class&gt;com.something.DbClass1&lt;/class&gt;
      &lt;class&gt;com.something.DbClass2&lt;/class&gt;
      &lt;exclude-unlisted-classes/&gt;
      &lt;properties&gt;
          &lt;property name="datanucleus.ConnectionDriverName" value="org.postgresql.Driver"/&gt;
          &lt;property name="datanucleus.ConnectionURL" value="jdbc:postgresql://IP:5432/some_db"/&gt;
          &lt;property name="datanucleus.ConnectionUserName" value="username"/&gt;
          &lt;property name="datanucleus.ConnectionPassword" value="password"/&gt;
          &lt;property name="datanucleus.connectionPoolingType" value="C3P0"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>I have also defined the C3P0.properties file :</p>
  
  <pre><code>c3p0.minPoolSize=15
  c3p0.maxPoolSize=100
  c3p0.initialPoolSize=15
  </code></pre>
  
  <p>I am using EntityManager like this :</p>
  
  <pre><code>EntityManager em = entityManager();
  DbClass1 x = em.merge(input);
  em.close();
  </code></pre>
  
  <p>Now, I have some oddities in the debug log. Connections seem to get opened/closed with each query (please see below):
  Is anybody able to point out what I'm doing wrong here ? Thanks !</p>
  
  <pre><code>13:13:43,969 DEBUG [DataNucleus.Persistence] - Adding support for Java type boolean (persistent=true, DFG=true, embedded=true)
  13:13:43,970 DEBUG [DataNucleus.Persistence] - Adding support for Java type byte (persistent=true, DFG=true, embedded=true)
  13:13:43,970 DEBUG [DataNucleus.Persistence] - Adding support for Java type char (persistent=true, DFG=true, embedded=true)
  ...
  13:13:44,738 INFO  [com.mchange.v2.c3p0.C3P0Registry] - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
  13:13:44,766 DEBUG [com.mchange.v2.c3p0.management.DynamicPooledDataSourceManagerMBean] - MBean: com.mchange.v2.c3p0:type=PooledDataSource[1br5qth8q1lseuas1a25ohv|38d0357a] registered.
  13:13:44,781 DEBUG [DataNucleus.Connection] - Created tx data source using pooling type of C3P0
  13:13:44,781 DEBUG [DataNucleus.Connection] - Registered transactional connection factory under name "rdbms/tx"
  13:13:44,796 DEBUG [com.mchange.v2.c3p0.management.DynamicPooledDataSourceManagerMBean] - MBean: com.mchange.v2.c3p0:type=PooledDataSource[1br5qth8q1lseuas1a25ohv|2415a94f] registered.
  13:13:44,796 DEBUG [DataNucleus.Connection] - Created nontx data source using pooling type of C3P0
  13:13:44,796 DEBUG [DataNucleus.Connection] - Registered nontransactional connection factory under name "rdbms/nontx"
  13:13:44,915 INFO  [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -&gt; 3, acquireRetryAttempts -&gt; 30, acquireRetryDelay -&gt; 1000, autoCommitOnClose -&gt; false, automaticTestTable -&gt; null, breakAfterAcquireFailure -&gt; false, checkoutTimeout -&gt; 0, connectionCustomizerClassName -&gt; null, connectionTesterClassName -&gt; com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -&gt; 1br5qth8q1lseuas1a25ohv|2415a94f, debugUnreturnedConnectionStackTraces -&gt; false, description -&gt; null, driverClass -&gt; org.postgresql.Driver, factoryClassLocation -&gt; null, forceIgnoreUnresolvedTransactions -&gt; false, identityToken -&gt; 1br5qth8q1lseuas1a25ohv|2415a94f, idleConnectionTestPeriod -&gt; 0, initialPoolSize -&gt; 15, jdbcUrl -&gt; jdbc:postgresql://IP:5432/some_db, maxAdministrativeTaskTime -&gt; 0, maxConnectionAge -&gt; 0, maxIdleTime -&gt; 3600, maxIdleTimeExcessConnections -&gt; 0, maxPoolSize -&gt; 100, maxStatements -&gt; 0, maxStatementsPerConnection -&gt; 0, minPoolSize -&gt; 15, numHelperThreads -&gt; 3, numThreadsAwaitingCheckoutDefaultUser -&gt; 0, preferredTestQuery -&gt; null, properties -&gt; {user=******, password=******}, propertyCycle -&gt; 0, testConnectionOnCheckin -&gt; false, testConnectionOnCheckout -&gt; false, unreturnedConnectionTimeout -&gt; 0, usesTraditionalReflectiveProxies -&gt; false ]
  13:13:45,201 DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@2a801059.acquireResource() returning. 
  13:13:45,203 DEBUG [DataNucleus.Connection] - Connection "com.mchange.v2.c3p0.impl.NewProxyConnection@695cd9c0" opened
  13:13:45,205 DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@2a801059.acquireResource() returning. 
  13:13:45,205 DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@2a801059.acquireResource() returning. 
  13:13:45,308 DEBUG [DataNucleus.Datastore] - Adding RDBMS support for Java type java.lang.Boolean (jdbc-type=BIT, sql-type=BIT, datastore-mapping-type=org.datanucleus.store.rdbms.mapping.BitRDBMSMapping, default=true)
  13:13:45,309 DEBUG [DataNucleus.Datastore] - Adding RDBMS support for Java type java.lang.Boolean (jdbc-type=CHAR, sql-type=CHAR, datastore-mapping-type=org.datanucleus.store.rdbms.mapping.CharRDBMSMapping, default=false)
  13:13:45,310 DEBUG [DataNucleus.Datastore] - Adding RDBMS support for Java type java.lang.Boolean (jdbc-type=BOOLEAN, sql-type=BOOLEAN, datastore-mapping-type=org.datanucleus.store.rdbms.mapping.BooleanRDBMSMapping, default=false)
  13:13:45,311 DEBUG [DataNucleus.Datastore] - Adding RDBMS support for Java type java.lang.Boolean (jdbc-type=SMALLINT, sql-type=SMALLINT, datastore-mapping-type=org.datanucleus.store.rdbms.mapping.SmallIntRDBMSMapping, default=false)
  ...
  13:14:11,791 DEBUG [DataNucleus.Persistence] - Object "&lt;redacted&gt;@52c51614" being inserted into table "&lt;redacted&gt;""
  13:14:11,791 DEBUG [DataNucleus.Connection] - Connection added to the pool : [org.datanucleus.store.rdbms.ConnectionFactoryImpl$ManagedConnectionImpl@1b78efd8, null] for key=org.datanucleus.ObjectManagerImpl@699c9f16 in factory=ConnectionFactory:nontx[org.datanucleus.store.rdbms.ConnectionFactoryImpl@29422384]
  13:14:11,791 DEBUG [DataNucleus.Connection] - Setting autocommit=false to connection: com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a
  13:14:11,842 DEBUG [DataNucleus.Connection] - Connection "com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a" opened with isolation level "read-committed"
  13:14:11,843 DEBUG [DataNucleus.Datastore.Persist] - Retrieving PreparedStatement for connection "com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a"
  13:14:11,843 DEBUG [DataNucleus.Datastore.Native] - INSERT INTO &lt;redacted&gt; ...
  13:14:11,843 DEBUG [DataNucleus.Datastore.Native] - INSERT INTO &lt;redacted&gt; ...
  13:14:11,897 DEBUG [DataNucleus.Datastore.Persist] - Execution Time = 53 ms (number of rows = 1)
  13:14:11,897 DEBUG [DataNucleus.Persistence] - Insert of object "&lt;redacted&gt;" is calling insertPostProcessing for field "&lt;redacted&gt;"
  13:14:11,897 DEBUG [DataNucleus.Datastore.Retrieve] - Closing PreparedStatement org.datanucleus.store.rdbms.ParamLoggingPreparedStatement@3970ae0
  13:14:11,990 DEBUG [DataNucleus.Connection] - Connection "com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a" non enlisted to a transaction is being committed.
  13:14:11,990 DEBUG [DataNucleus.Connection] - Connection "com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a" closed
  13:14:11,990 DEBUG [DataNucleus.Connection] - Connection removed from the pool : [org.datanucleus.store.rdbms.ConnectionFactoryImpl$ManagedConnectionImpl@1b78efd8, com.mchange.v2.c3p0.impl.NewProxyConnection@455dd32a] for key=org.datanucleus.ObjectManagerImpl@699c9f16 in factory=ConnectionFactory:nontx[org.datanucleus.store.rdbms.ConnectionFactoryImpl@29422384]
  </code></pre>