fixed broken persistence implementation after refactor generalization

This commit is contained in:
2019-01-18 14:28:35 +01:00
parent 2659ba1ff5
commit 1aab667a8e
3 changed files with 57 additions and 27 deletions

View File

@ -1,6 +1,7 @@
package de.muehlencord.shared.account.web;
import de.muehlencord.shared.account.util.AccountPU;
import de.muehlencord.shared.account.util.ApplicationPU;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
@ -21,24 +22,50 @@ public class PersistenceContextFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(PersistenceContextFactory.class);
// need to define 2nd database as TransactionJoinInterceptor requires it
// account UI is the only application where application and account is the same database
// account UI does not call this as it references all database access via accountPu
@PersistenceUnit (unitName = "accountPu")
EntityManagerFactory emf;
@Produces
@AccountPU
EntityManagerFactory applicationEntityManagerFactory;
@PersistenceUnit (unitName = "accountPu")
EntityManagerFactory accountEntityManagerFactory;
@Produces
@RequestScoped
public EntityManager getEntityManager() {
@ApplicationPU
public EntityManager getPcdEntityManager() {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("getting entityManager for accountPu");
}
return emf.createEntityManager(SynchronizationType.UNSYNCHRONIZED);
LOGGER.trace("getting entityManager for application");
}
EntityManager em = applicationEntityManagerFactory.createEntityManager(SynchronizationType.UNSYNCHRONIZED);
return em;
}
public void closeEntityManager(@Disposes @AccountPU EntityManager em) {
public void closePcdEntityManager (@Disposes @ApplicationPU EntityManager em) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("closing entityManager");
}
LOGGER.trace("closing entityManager application database");
}
em.close();
}
}
@Produces
@RequestScoped
@AccountPU
public EntityManager getAccountEntityManager() {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("getting entityManager for account database");
}
EntityManager em = accountEntityManagerFactory.createEntityManager(SynchronizationType.UNSYNCHRONIZED);
return em;
}
public void closeAccountEntityManager (@Disposes @AccountPU EntityManager em) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("closing entityManager for account database");
}
em.close();
}
}

View File

@ -33,10 +33,10 @@ import org.slf4j.LoggerFactory;
*/
@Transactional(value = REQUIRED)
@Interceptor
@Priority(value=TransactionJoinInterceptor.PRIORITY)
public class TransactionJoinInterceptor {
@Priority(value=AccountTransactionJoinInterceptor.PRIORITY)
public class AccountTransactionJoinInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionJoinInterceptor.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AccountTransactionJoinInterceptor.class);
// attach behind the interceptor of the container
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE+250;

View File

@ -1,6 +1,5 @@
package de.muehlencord.shared.account.business;
package de.muehlencord.shared.account.util;
import de.muehlencord.shared.account.util.ApplicationPU;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
@ -9,36 +8,40 @@ import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import static javax.transaction.Transactional.TxType.REQUIRED;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
@Transactional(value = REQUIRED)
@Interceptor
@Priority(value = TransactionJoinInterceptor.PRIORITY)
public class TransactionJoinInterceptor {
@Priority(value = ApplicationTransactionJoinInterceptor.PRIORITY)
public class ApplicationTransactionJoinInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionJoinInterceptor.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationTransactionJoinInterceptor.class);
// attach behind the interceptor of the container
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE + 250;
@Inject
@NotNull
@ApplicationPU
private EntityManager em;
@AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception {
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
if (em == null) {
return context.proceed();
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
}
return context.proceed();
}
}