4
votes

I have read a lot of other questions at stackoverflow concerning my problem but I didn’t find a proper solution.

I development an OSGI Application (Equinox) and use JPA (EclipseLink). Now I ask for the best way to get the EntityManager in the bundles which require it. When I publish the EntityManagerFactory as service and use it to get the EntityManger I have to provide the persistence.xml in every bundle where I use the EntityManager. When I write an utitlity class sharing one instance of an EntityManger and publish it as service I am afraid to run into thread synchronization problems.

Is there any kind of best practices to provide the EntityManager when using OSGI and JPA?

Thanks

Dan

2

2 Answers

1
votes

In EclipseLink when used within OSGi you have to declare in which bundle the persistence.xml could be found by adding “JPA-PersistenceUnits: myPersistenceUnit” to the bundle’s MANIFEST.MF in which the persistence.xml is located. EclipseLink will search for this declaration and will use the class loader of this bundle to get the persistence.xml. Then you can publish the EntityManagerFactory as Service and don’t need to share the persistence.xml in every bundle using the service. But I think this only works with EclipseLink.

0
votes

I used this blog to get setup with OpenJPA and separate model bundles without each client bundle knowing about persistence.xml. Since EclipseLink is a JPA provider, it should be roughly the same setup. Be sure to look at the example code that is linked at the bottom of the blog.