문제 설명
OSGI 애플리케이션에서 EntityManager를 제공하는 모범 사례 (Best practice to provide EntityManager in OSGI applications)
제 문제와 관련하여 stackoverflow에서 다른 많은 질문을 읽었지만 적절한 해결책을 찾지 못했습니다.
저는 OSGI 애플리케이션(Equinox)을 개발하고 JPA(EclipseLink)를 사용합니다. 이제 필요한 번들에서 EntityManager를 얻는 가장 좋은 방법을 묻습니다. EntityManagerFactory를 서비스로 게시하고 이를 사용하여 EntityManger를 얻을 때 EntityManager를 사용하는 모든 번들에 persistence.xml을 제공해야 합니다. EntityManger의 한 인스턴스를 공유하는 유틸리티 클래스를 작성하고 이를 서비스로 게시할 때 스레드 동기화 문제가 발생할까봐 두렵습니다.
OSGI 및 JPA를 사용할 때 EntityManager를 제공하는 모범 사례가 있습니까? ?
고마워
댄
참조 솔루션
방법 1:
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.
방법 2:
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.