Best practices for providing EntityManager in OSGI applications

I read many other questions in stackoverflow regarding my problem, but I did not find the right solution.

I am developing an OSGI application (Equinox) and am using JPA (EclipseLink). Now I ask for the best way to get EntityManager in packages that require it. When I publish the EntityManagerFactory service as a service and use it to get EntityManger, I have to provide persistence.xml in every bundle where I use EntityManager. When I write a utitlity class that shares a single EntityManger instance and publishes it as a service, I am afraid to run into thread synchronization issues.

Are there any recommendations for providing EntityManager when using OSGI and JPA?

thanks

Dan

+4
source share
2 answers

In EclipseLink, when used in OSGi, you must declare which persistence.xml package can be found in by adding " JPA-PersistenceUnits: myPersistenceUnit " to the MANIFEST.MF packages in which it is saved. xml. EclipseLink will look for this declaration and will use the class loader of this package to obtain persistence.xml. You can then publish the EntityManagerFactory as a Service and should not share the persistence.xml file in each bundle using the service. But I think this only works with EclipseLink.

+1
source

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

0
source

All Articles