[
https://issues.apache.org/jira/browse/DELTASPIKE-816?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14542197#comment-14542197
]
Gerhard Petracek edited comment on DELTASPIKE-816 at 5/13/15 4:44 PM:
----------------------------------------------------------------------
@ron:
#1
no - the jpa module is e.g. about
@org.apache.deltaspike.jpa.api.transaction.Transactional and those producers
are needed (one approach of those) if you would like to use @Transactional with
multiple persistence-units.
#2
our documentation covers EntityManagerFactory already. that's fine if you need
a managed entity-manager... however, this section should be just about handling
transactions in case of multiple entity-manages with
@org.apache.deltaspike.jpa.api.transaction.Transactional (everything else
should be in place already).
was (Author: gpetracek):
@ron:
no the jpa module is e.g. about
@org.apache.deltaspike.jpa.api.transaction.Transactional and those producers
are needed (one of those) if you would like to use @Transactional with multiple
persistence-units.
> 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)