fixed broken persistence implementation after refactor generalization
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package de.muehlencord.shared.account.web;
|
package de.muehlencord.shared.account.web;
|
||||||
|
|
||||||
import de.muehlencord.shared.account.util.AccountPU;
|
import de.muehlencord.shared.account.util.AccountPU;
|
||||||
|
import de.muehlencord.shared.account.util.ApplicationPU;
|
||||||
import javax.enterprise.context.ApplicationScoped;
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.enterprise.inject.Disposes;
|
import javax.enterprise.inject.Disposes;
|
||||||
@ -21,22 +22,48 @@ public class PersistenceContextFactory {
|
|||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(PersistenceContextFactory.class);
|
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")
|
@PersistenceUnit (unitName = "accountPu")
|
||||||
EntityManagerFactory emf;
|
EntityManagerFactory applicationEntityManagerFactory;
|
||||||
|
|
||||||
|
@PersistenceUnit (unitName = "accountPu")
|
||||||
|
EntityManagerFactory accountEntityManagerFactory;
|
||||||
|
|
||||||
@Produces
|
@Produces
|
||||||
@AccountPU
|
|
||||||
@RequestScoped
|
@RequestScoped
|
||||||
public EntityManager getEntityManager() {
|
@ApplicationPU
|
||||||
|
public EntityManager getPcdEntityManager() {
|
||||||
if (LOGGER.isTraceEnabled()) {
|
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()) {
|
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();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,10 +33,10 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
@Transactional(value = REQUIRED)
|
@Transactional(value = REQUIRED)
|
||||||
@Interceptor
|
@Interceptor
|
||||||
@Priority(value=TransactionJoinInterceptor.PRIORITY)
|
@Priority(value=AccountTransactionJoinInterceptor.PRIORITY)
|
||||||
public class TransactionJoinInterceptor {
|
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
|
// attach behind the interceptor of the container
|
||||||
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE+250;
|
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.annotation.Priority;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.interceptor.AroundInvoke;
|
import javax.interceptor.AroundInvoke;
|
||||||
@ -9,36 +8,40 @@ import javax.interceptor.InvocationContext;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import static javax.transaction.Transactional.TxType.REQUIRED;
|
import static javax.transaction.Transactional.TxType.REQUIRED;
|
||||||
import javax.validation.constraints.NotNull;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Joern Muehlencord <joern at muehlencord.de>
|
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||||
*/
|
*/
|
||||||
@Transactional(value = REQUIRED)
|
@Transactional(value = REQUIRED)
|
||||||
@Interceptor
|
@Interceptor
|
||||||
@Priority(value = TransactionJoinInterceptor.PRIORITY)
|
@Priority(value = ApplicationTransactionJoinInterceptor.PRIORITY)
|
||||||
public class TransactionJoinInterceptor {
|
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
|
// attach behind the interceptor of the container
|
||||||
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE + 250;
|
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE + 250;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@NotNull
|
|
||||||
@ApplicationPU
|
@ApplicationPU
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
@AroundInvoke
|
@AroundInvoke
|
||||||
public Object joinTransaction(InvocationContext context) throws Exception {
|
public Object joinTransaction(InvocationContext context) throws Exception {
|
||||||
|
if (em == null) {
|
||||||
|
return context.proceed();
|
||||||
|
} else {
|
||||||
if (em.isJoinedToTransaction()) {
|
if (em.isJoinedToTransaction()) {
|
||||||
LOGGER.trace("transaction already joined");
|
LOGGER.trace("transaction already joined");
|
||||||
} else {
|
} else {
|
||||||
LOGGER.trace("joining transaction");
|
LOGGER.trace("joining transaction");
|
||||||
em.joinTransaction();
|
em.joinTransaction();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return context.proceed();
|
return context.proceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user