====== Connection Pooling with JPA and Apache Karaf ======
This has been tested with Apache Karaf 3.0.3 and by installing the following features:
feature:install scr jpa/2.0.0 openjpa/2.2.2 transaction jndi jdbc
===== JDBC Driver =====
PostgreSQL is used in this example. The driver can be retrieved as a bundle from the Apache Servicemix project. Look in the [[http://search.maven.org | Maven Repository]] for ''org.apache.servicemix.bundles.postgresql''.
Just drop the bundle into the ''deploy'' folder and set the bundle start level to 25.
I think the low bundle level is necessary because it the driver need to be started before the Aries JPA stuff so that it can pick up any Drivers. But I might be wrong with this one.
===== DataSource =====
The ''javax.sql.DataSource'' instances will be created via Blueprint. Create a ''OSGI-INF/blueprint/blueprint.xml'' file in your project which looks like this:
If you have any questions about this file, look at the OSGi Blueprint specs.
''BasicManagedDataSource'' is packaged in the ''commons-dbcp2'' project (which has a dependency to ''commons-pool2''). Both projects are already packaged as bundles and can be fetched from the maven repository.
The placeholders are filled by the ConfigAdmin service. It uses the configuration file //sdm.persistence.datasource// as stated in the attribute ''persistent-id'' which resides in the ''etc'' folder of Karaf. It may look something like this:
service.pid=sdm.persistence.datasource
host=server
database=my_db
user=sgbs
password=sgbs
The DataSource instances can be checked via
service:list DataSource
===== JPA =====
Now the JPA needs to use our DataSource instances for getting a connection. This can be done in the ''persistence.xml'' file.
It may look something like this:
org.apache.openjpa.persistence.PersistenceProviderImpl
osgi:service/jdbc/xasds
osgi:service/jdbc/sds
my.jpa.EntityClass
Transaction testing still needs to be done!
===== Other Solutions =====
==== Pax JDBC ====
The project [[https://ops4j1.jira.com/wiki/display/PAXJDBC | Pax JDBC]] offers a nice solution to for creating DataSource objects via ConfigAdmin and also supports connection pooling.