fixed broken persistence implementation after refactor generalization
This commit is contained in:
@ -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,22 +22,48 @@ 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;
|
||||
EntityManagerFactory applicationEntityManagerFactory;
|
||||
|
||||
@PersistenceUnit (unitName = "accountPu")
|
||||
EntityManagerFactory accountEntityManagerFactory;
|
||||
|
||||
@Produces
|
||||
@AccountPU
|
||||
@RequestScoped
|
||||
public EntityManager getEntityManager() {
|
||||
@ApplicationPU
|
||||
public EntityManager getPcdEntityManager() {
|
||||
if (LOGGER.isTraceEnabled()) {
|
||||
LOGGER.trace("getting entityManager for accountPu");
|
||||
LOGGER.trace("getting entityManager for application");
|
||||
}
|
||||
return emf.createEntityManager(SynchronizationType.UNSYNCHRONIZED);
|
||||
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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user