在 OSGI 应用程序中提供 EntityManager 的最佳实践
我在 stackoverflow 上阅读了很多有关我的问题的其他问题,但没有找到合适的解决方案。
我开发了一个 OSGI 应用程序 (Equinox) 并使用 JPA (EclipseLink)。现在我询问在需要它的包中获取 EntityManager 的最佳方法。当我将 EntityManagerFactory 作为服务发布并使用它来获取 EntityManger 时,我必须在使用 EntityManager 的每个包中提供 persistence.xml。当我编写一个共享 EntityManger 的一个实例的实用程序类并将其发布为服务时,我担心会遇到线程同步问题。
使用 OSGI 和 JPA 时是否有提供 EntityManager 的最佳实践?
谢谢丹
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 EclipseLink 中,当在 OSGi 中使用时,您必须通过将“JPA-PersistenceUnits: myPersistenceUnit”添加到 persistence.xml 所在的包的 MANIFEST.MF 来声明可以在哪个包中找到 persistence.xml 。 EclipseLink 将搜索此声明并使用此包的类加载器来获取 persistence.xml。然后,您可以将 EntityManagerFactory 发布为服务,而无需在使用该服务的每个包中共享 persistence.xml。但我认为这只适用于 EclipseLink。
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.
我使用 此博客 来设置 OpenJPA 和单独的模型包无需每个客户端包都知道 persistence.xml。由于 EclipseLink 是 JPA 提供程序,因此它的设置应该大致相同。请务必查看示例代码 该链接位于博客底部。
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.