开发者

Best practice to provide EntityManager in OSGI applications

开发者 https://www.devze.com 2023-01-26 17:45 出处:网络
I have read a lot of other questions at stackoverflow concerning my problem but I didn’t find a proper solution.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消