fixed tx join interceptor handling

This commit is contained in:
2019-04-27 14:07:31 +01:00
parent 70829f9204
commit 4f3c3d4673
3 changed files with 21 additions and 46 deletions

View File

@ -15,10 +15,13 @@
*/ */
package de.muehlencord.shared.account.business; package de.muehlencord.shared.account.business;
import javax.ejb.ApplicationException;
/** /**
* *
* @author joern.muehlencord * @author joern.muehlencord
*/ */
@ApplicationException(rollback=true)
public class ControllerException extends Exception { public class ControllerException extends Exception {
private static final long serialVersionUID = 5190280225284514859L; private static final long serialVersionUID = 5190280225284514859L;

View File

@ -15,7 +15,6 @@
*/ */
package de.muehlencord.shared.account.util; package de.muehlencord.shared.account.util;
import de.muehlencord.shared.account.business.ControllerException;
import javax.annotation.Priority; import javax.annotation.Priority;
import javax.inject.Inject; import javax.inject.Inject;
import javax.interceptor.AroundInvoke; import javax.interceptor.AroundInvoke;
@ -27,50 +26,36 @@ import static javax.transaction.Transactional.TxType.REQUIRED;
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=AccountTransactionJoinInterceptor.PRIORITY) @Priority(value = AccountTransactionJoinInterceptor.PRIORITY)
public class AccountTransactionJoinInterceptor { public class AccountTransactionJoinInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(AccountTransactionJoinInterceptor.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;
@Inject @Inject
@AccountPU @AccountPU
private EntityManager em; private EntityManager em;
@AroundInvoke @AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception { public Object joinTransaction(InvocationContext context) throws Exception {
try { if (em == null) {
if (em == null) {
return context.proceed();
} else {
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
}
return context.proceed(); return context.proceed();
} catch (ControllerException ex) { } else {
if (em.isJoinedToTransaction()) { if (em.isJoinedToTransaction()) {
em.getTransaction().rollback(); LOGGER.trace("transaction already joined");
if (LOGGER.isTraceEnabled()) { } else {
LOGGER.trace("Transaction rolled back"); LOGGER.trace("joining transaction");
} em.joinTransaction();
} }
throw ex;
} catch (Exception ex) {
throw ex;
} }
return context.proceed();
} }
} }

View File

@ -1,6 +1,5 @@
package de.muehlencord.shared.account.util; package de.muehlencord.shared.account.util;
import de.muehlencord.shared.account.business.ControllerException;
import javax.annotation.Priority; import javax.annotation.Priority;
import javax.inject.Inject; import javax.inject.Inject;
import javax.interceptor.AroundInvoke; import javax.interceptor.AroundInvoke;
@ -32,28 +31,16 @@ public class ApplicationTransactionJoinInterceptor {
@AroundInvoke @AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception { public Object joinTransaction(InvocationContext context) throws Exception {
try { if (em == null) {
if (em == null) {
return context.proceed();
} else {
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
}
return context.proceed(); return context.proceed();
} catch (ControllerException ex) { } else {
if (em.isJoinedToTransaction()) { if (em.isJoinedToTransaction()) {
em.getTransaction().rollback(); LOGGER.trace("transaction already joined");
if (LOGGER.isTraceEnabled()) { } else {
LOGGER.trace("Transaction rolled back"); LOGGER.trace("joining transaction");
} em.joinTransaction();
} }
throw ex;
} catch (Exception ex) {
throw ex;
} }
return context.proceed();
} }
} }