ensured transaction is rolled back in case a ControllerException occurs

This commit is contained in:
2019-02-06 16:55:37 +01:00
parent 479145af6f
commit 6117cc6c10
3 changed files with 46 additions and 17 deletions

View File

@ -19,7 +19,7 @@ package de.muehlencord.shared.account.business;
* *
* @author joern.muehlencord * @author joern.muehlencord
*/ */
public class ControllerException extends RuntimeException { public class ControllerException extends Exception {
private static final long serialVersionUID = 5190280225284514859L; private static final long serialVersionUID = 5190280225284514859L;
public static final int CAUSE_ALREADY_EXISTS = 1; public static final int CAUSE_ALREADY_EXISTS = 1;

View File

@ -15,6 +15,7 @@
*/ */
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;
@ -46,14 +47,30 @@ public class AccountTransactionJoinInterceptor {
private EntityManager em; private EntityManager em;
@AroundInvoke @AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception { public Object joinTransaction(InvocationContext context) throws Exception {
if (em.isJoinedToTransaction()) { try {
LOGGER.trace("transaction already joined"); if (em == null) {
} else { return context.proceed();
LOGGER.trace("joining transaction"); } else {
em.joinTransaction(); if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
}
return context.proceed();
} catch (ControllerException ex) {
if (em.isJoinedToTransaction()) {
em.getTransaction().rollback();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transaction rolled back");
}
}
throw ex;
} catch (Exception ex) {
throw ex;
} }
return context.proceed();
} }
} }

View File

@ -1,5 +1,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;
@ -31,17 +32,28 @@ public class ApplicationTransactionJoinInterceptor {
@AroundInvoke @AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception { public Object joinTransaction(InvocationContext context) throws Exception {
if (em == null) { try {
return context.proceed(); if (em == null) {
} else { return context.proceed();
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else { } else {
LOGGER.trace("joining transaction"); if (em.isJoinedToTransaction()) {
em.joinTransaction(); LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
} }
return context.proceed();
} catch (ControllerException ex) {
if (em.isJoinedToTransaction()) {
em.getTransaction().rollback();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transaction rolled back");
}
}
throw ex;
} catch (Exception ex) {
throw ex;
} }
return context.proceed();
} }
} }