title: Eclipselink, c3p0 and Spring - creating too many connections!
body: <p>I'm trying to use <strong>c3p0</strong> to pool my connections to a MySQL DB with <strong>Eclipselink</strong>, but I've encountered a problem. On starting the <strong>Virgo</strong> server, the correct number of c3p0 initialPoolSize connections are created, but everytime an <strong>EntityTransaction</strong> is used a further connection is created - even up and beyond the c3p0 set maxPoolSize. </p>
  
  <p>Obviously this is a issue as the max connections is quickly met, but being relatively new to this setup I'm finding it difficult to identify where the error is.. I've attached the configuration files I am using, hoping that one of you can see where I'm introducing an error!</p>
  
  <p>The JPA used to save an object is:</p>
  
  <pre><code>public class JpaTbRepository {
  
      private final EntityManagerFactory emf;    
      public JpaTbRepository(EntityManagerFactoryBuilder builder,
                                      Map&lt;String, Object&gt; properties) {
          this.emf = builder.createEntityManagerFactory(properties);
      }
  
      public JpaTbRepository(EntityManagerFactory entityManagerFactory) {
          this.emf = entityManagerFactory;
      }
  
      @Override
      public void save(TbObject tb) {      
  
          EntityManager em = emf.createEntityManager();
          try {
              em.getTransaction().begin();
              TbObjectEntity tbEntity = TbObjectEntity.valueOf(tb);
              em.persist(tbEntity);
              em.getTransaction().commit();
          } finally {
              em.close();
          }
      }
  }
  </code></pre>
  
  <p>The application context is then:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:p="http://www.springframework.org/schema/p" 
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                             http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd"&gt;
  
      &lt;!--
          Activates various annotations to be detected in bean classes: Spring's
          @Required and @Autowired, as well as JSR 250's @PostConstruct,
          @PreDestroy and @Resource (if available) and JPA's @PersistenceContext
          and @PersistenceUnit (if available).
      --&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;context:property-placeholder properties-ref="config" ignore-unresolvable="true" system-properties-mode="NEVER"  /&gt;
      &lt;osgix:cm-properties id="config" persistent-id="org.olanb" /&gt;
  
      &lt;bean id="databaseConfig" class="org.olanb.ConfigurationPropertyPlaceholderConfigurer"&gt;
          &lt;property name="queryPath" value="Database" /&gt;
          &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
          &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="connPoolConfig" class="org.olanb.ConfigurationPropertyPlaceholderConfigurer"&gt;
          &lt;property name="queryPath" value="Database/ConnectionPool" /&gt;
          &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
          &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" 
            class="com.mchange.v2.c3p0.ComboPooledDataSource" 
            destroy-method="close"
            p:driverClass="com.mysql.jdbc.Driver"
            p:jdbcUrl="jdbc:mysql://${PrimaryHost},${SecondaryHost}/${Schema}?autoReconnect=true&amp;amp;failOverReadOnly=false"
            p:user="user"
            p:password="pass"
            p:initialPoolSize="5" 
            p:minPoolSize="5" 
            p:maxPoolSize="10"
            p:maxStatements="20" /&gt;           
  
      &lt;bean id="tbRepository" class="org.olanb.JpaTbRepository"&gt;
        &lt;constructor-arg ref="entityFactoryBuilder" /&gt;
        &lt;constructor-arg ref="databaseProperties" /&gt;
      &lt;/bean&gt;
  
      &lt;util:map id="databaseProperties" map-class="java.util.HashMap"&gt;
          &lt;entry key="javax.persistence.nonJtaDataSource" value-ref="dataSource"/&gt;
          &lt;entry key="eclipselink.target-database" value="MySQL" /&gt;
          &lt;entry key="eclipselink.jdbc.read-connections.min" value="1" /&gt;
          &lt;entry key="eclipselink.jdbc.write-connections.min" value="1" /&gt;
          &lt;entry key="eclipselink.jdbc.batch-writing" value="JDBC" /&gt;
          &lt;entry key="eclipselink.ddl-generation" value="create-tables" /&gt;
          &lt;entry key="eclipselink.ddl-generation.output-mode" value="database" /&gt;
          &lt;entry key="eclipselink.logging.level" value="INFO" /&gt;
          &lt;entry key="eclipselink.logging.thread" value="false" /&gt;
          &lt;entry key="eclipselink.logging.session" value="false" /&gt;
          &lt;entry key="eclipselink.logging.exceptions" value="true" /&gt;
          &lt;entry key="eclipselink.logging.timestamp" value="false" /&gt;
          &lt;entry key="eclipselink.cache.shared.default" value="false" /&gt;
      &lt;/util:map&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Also, this is being used in an OSGi  bundle so the OSGi context xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;bean:beans xmlns="http://www.springframework.org/schema/osgi"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:bean="http://www.springframework.org/schema/beans"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                              http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"&gt;
  
  
  &lt;service ref="tbRepository" interface="org.olanb.api.tbRepository"/&gt;
  
  &lt;service ref="catalog" interface="org.olanb.Catalog" /&gt;
  
  &lt;reference id="entityFactoryBuilder" 
             interface="org.osgi.service.jpa.EntityManagerFactoryBuilder" 
             filter="(osgi.unit.name=olanb.jpa)"&gt;
  &lt;/reference&gt;
  </code></pre>
  
  <p></p>
  
  <p>And finally the persistence.xml looks something like:</p>
  
  <pre><code>&lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
      &lt;persistence-unit name="olanb.jpa"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;class&gt;org.olanb.tbEntity&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>