You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltaspike.apache.org by "Ron Smeral (JIRA)" <ji...@apache.org> on 2015/06/01 12:11:17 UTC
[jira] [Resolved] (DELTASPIKE-816) document usage of multiple
entity-managers
[ https://issues.apache.org/jira/browse/DELTASPIKE-816?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Ron Smeral resolved DELTASPIKE-816.
-----------------------------------
Resolution: Fixed
Pushed and published.
https://git-wip-us.apache.org/repos/asf?p=deltaspike.git;a=commit;h=087bb799ac8afc1a5c60986a31e11eb02db0a1e1
http://deltaspike.apache.org/documentation/jpa.html#@Transactional
> 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.1
>
>
> #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)