DB connections not closing in OSGi -
i have osgi bundle needs persist data in database. described in previous stackoverflow question have found in order transactions work expected need use xadatasource connect database. when see connections database opened application never closed, of course results in database not being able accept more connections after while.
my setup following:
i have bundle creates datasource , includes blueprint.xml file following content
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="datasource" class="com.mysql.jdbc.jdbc2.optional.mysqldatasource"> <property name="url" value="jdbc:mysql://localhost:3306/myschema"/> <property name="user" value="user"/> <property name="password" value="pass"/> </bean> <service interface="javax.sql.xadatasource" ref="datasource"> <service-properties> <entry key="osgi.jndi.service.name" value="jdbc/mysqlds"/> </service-properties> </service> </blueprint>
then in bundle persists data have persistence.xml
<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"> <persistence-unit name="mypu" transaction-type="jta"> <jta-data-source>osgi:service/javax.sql.datasource/(osgi.jndi.service.name=jdbc/mysqlds) </jta-data-source> </persistence-unit> </persistence>
and specify service methods should run in transaction in blueprint.xml
<bean id="myserviceimpl" class="com.test.impl.myserviceimpl"> <jpa:context property="em" unitname="mypu" /> <tx:transaction method="*" value="required" /> </bean> <service id="myservice" ref="myserviceimpl" interface="com.test.api.myservice" />
i deploy bundles in karaf, using aries , openjpa persistence, while have deployed aries transaction wrapper bundle (org.apache.aries.transaction.wrappers) in order enlist xa resources transaction manager.
any ideas missing in configuration?
edit: after more searching found this dbcp issue suggests problem i'm having bug of dbcp mysql. i'm @ loss on how replace dbcp other connection pool implementation openjpa can work with. suggestions more welcome.
i used commons-dbcp have connection pool enlists xa connections following configuration:
<bean id="myxaenabledconnectionpooldatasource" class="org.apache.commons.dbcp.managed.basicmanageddatasource" destroy-method="close"> <property name="xadatasourceinstance" ref="mysqlxadatasourcebean" /> <property name="transactionmanager" ref="transactionmanager" /> </bean>
you can transaction manager reference based on interface javax.transaction.transactionmanager.
in way commons-dbcp handle lifecycle of connections properly. please note destroy method there when blueprint container stops connection pool closed.
edit:
1-2 years ago had same problem postgresql. debugged aries.transaction.wrapper @ time lot cannot remember cause why left out. think motivation behind commons-dbcp solution worked me in previous projects while not fix aries.transaction.wrapper after analyzing it's code lot.
please note mysqldatasource not connection pool. gives new connection when need one. not xa enabled. mysqlxadatasource xa enabled should instantiate object class. however, xadatasource responsible give xaconnections not enlisting them. managedconnectionpool can help. managed connection pool followings:
- wraps provided connection objects custom managed connection class
- in case close called on connection, not closed if there ongoing transaction. closed (added pool) when transaction commit or rollback done
- in case connection queried pool , there connection provided in same transaction, same transaction returned (that difficult sentence :))
sometimes jdbc drivers provide connection pools , managed connection pools, however, better use jdbc driver new connections , wrap 3rdparty library tested in several projects , works sure.
Comments
Post a Comment