[
https://issues.apache.org/jira/browse/DELTASPIKE-816?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14542029#comment-14542029
]
Ron Smeral commented on DELTASPIKE-816:
---------------------------------------
I think this does not belong to DS docs. This is not documentation of a DS
feature. It's just CDI+JPA knowledge. We show some solutions in the docs, but
only ones related directly to DS.
To me, this looks almost like a CDI exercise: find 3 ways to produce 2
different EMs ;) The first one uses two producers, the second one decides based
on the IP, and the third one decides on some kind of external property.
The only part I'd document is that we can @Inject @PersistenceUnitName(..) EMF,
because there's a EntityManagerFactoryProducer in the JPA module.
> document usage of multiple entity-managers
> ------------------------------------------
>
> Key: DELTASPIKE-816
> URL: https://issues.apache.org/jira/browse/DELTASPIKE-816
> Project: DeltaSpike
> Issue Type: Task
> Components: Documentation
> Affects Versions: 1.2.1
> Reporter: Gerhard Petracek
> Assignee: Ron Smeral
> Fix For: 1.4.0
>
>
> #1) application-managed via @PersistenceUnitName:
> {code}
> @ApplicationScoped
> public class MultiEntityManagerProducer
> {
> @Inject
> @PersistenceUnitName("puA")
> private EntityManagerFactory emfA;
> @Inject
> @PersistenceUnitName("puB")
> private EntityManagerFactory emfB;
> @Produces
> //e.g.: @RequestScoped
> @DbA //custom qualifier annotation
> public EntityManager createEntityManagerA()
> {
> return emfA.createEntityManager();
> }
> public void closeEmA(@Disposes @DbA EntityManager em)
> {
> em.close();
> }
> @Produces
> //e.g.: @RequestScoped
> @DbB //custom qualifier annotation
> public EntityManager createEntityManagerB()
> {
> return emfB.createEntityManager();
> }
> public void closeEmB(@Disposes @DbB EntityManager em)
> {
> em.close();
> }
> }
> {code}
> or
> #2) select from multiple container-managed em-managers based on the
> InjectionPoint:
> {code}
> public class EntityManagerSelector
> {
> @PersistenceContext(unitName = "firstDB")
> private EntityManager firstEntityManager;
> @PersistenceContext(unitName = "secondDB")
> private EntityManager secondEntityManager;
> @Produces
> protected EntityManager createEntityManager(InjectionPoint injectionPoint)
> {
> CustomQualifier customQualifier =
> injectionPoint.getAnnotated().getAnnotation(CustomQualifier.class);
> return selectEntityManager(customQualifier); //selects
> firstEntityManager or secondEntityManager based on the details provided by
> CustomQualifier
> }
> //...
> }
> {code}
> or
> #3) select from multiple container-managed em-managers based on a custom
> context:
> {code}
> public class EntityManagerSelector
> {
> @PersistenceContext(unitName = "firstDB")
> private EntityManager firstEntityManager;
> @PersistenceContext(unitName = "secondDB")
> private EntityManager secondEntityManager;
> @Inject
> private CustomDatabaseContext customDatabaseContext;
> @Produces
> protected EntityManager createEntityManager()
> {
> if (customDatabaseContext.usePrimaryDb()) {
> return firstEntityManager;
> }
> return secondEntityManager;
> }
> //...
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)