title: DB connections not closing in OSGi
body: <p>I have an OSGi bundle which needs to persist data in a database. As described <a href="https://stackoverflow.com/questions/19031360/transaction-rollback-in-osgi">in a previous stackoverflow question</a> I have found that in order for transactions to work as expected I need to use an XADataSource to connect to the database. When I do so however I see that the connections to the database that are opened by my application are never closed, which of course results in the database not being able to accept any more connections after a while.</p>
  
  <p>My setup is the following:</p>
  
  <p>I have a bundle which creates the datasource and which only includes a blueprint.xml file with the following content</p>
  
  <pre><code>&lt;blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"&gt;
  
    &lt;bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"&gt;
        &lt;property name="url" value="jdbc:mysql://localhost:3306/myschema"/&gt;
        &lt;property name="user" value="user"/&gt;
        &lt;property name="password" value="pass"/&gt;
    &lt;/bean&gt;
  
    &lt;service interface="javax.sql.XADataSource" ref="dataSource"&gt;
      &lt;service-properties&gt;
              &lt;entry key="osgi.jndi.service.name" value="jdbc/mysqlds"/&gt;
      &lt;/service-properties&gt;
    &lt;/service&gt;
  &lt;/blueprint&gt;
  </code></pre>
  
  <p>Then in the bundle that persists my data I have a persistence.xml</p>
  
  <pre><code>&lt;persistence version="2.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_2_0.xsd"&gt;
  
      &lt;persistence-unit name="mypu" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/mysqlds)
          &lt;/jta-data-source&gt;
      &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And I specify that my service methods should run in a transaction in my blueprint.xml</p>
  
  <pre><code>&lt;bean id="MyServiceImpl"
            class="com.test.impl.MyServiceImpl"&gt;
      &lt;jpa:context property="em" unitname="mypu" /&gt;
      &lt;tx:transaction method="*" value="Required" /&gt;
  &lt;/bean&gt;
  &lt;service id="MyService" ref="MyServiceImpl" interface="com.test.api.MyService" /&gt; 
  </code></pre>
  
  <p>I deploy by bundles in Karaf, using Aries and OpenJPA for persistence, while I have also deployed the Aries transaction wrapper bundle (org.apache.aries.transaction.wrappers) in order to enlist my XA resources with the transaction manager.</p>
  
  <p>Any ideas what I am missing in my configuration?</p>
  
  <p><strong>Edit:</strong>
  After some more searching I found <a href="https://issues.apache.org/jira/browse/DBCP-355" rel="nofollow noreferrer">this DBCP issue</a> which suggests that the problem I'm having is a bug of DBCP with MySQL. However I'm at a loss on how to replace DBCP with some other Connection Pool implementation OpenJPA can work with. Any suggestions are more than welcome.</p>