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
*/
public class ControllerException extends RuntimeException {
public class ControllerException extends Exception {
private static final long serialVersionUID = 5190280225284514859L;
public static final int CAUSE_ALREADY_EXISTS = 1;

View File

@ -15,6 +15,7 @@
*/
package de.muehlencord.shared.account.util;
import de.muehlencord.shared.account.business.ControllerException;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
@ -48,12 +49,28 @@ public class AccountTransactionJoinInterceptor {
@AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception {
try {
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();
} catch (ControllerException ex) {
if (em.isJoinedToTransaction()) {
em.getTransaction().rollback();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transaction rolled back");
}
}
throw ex;
} catch (Exception ex) {
throw ex;
}
}
}

View File

@ -1,5 +1,6 @@
package de.muehlencord.shared.account.util;
import de.muehlencord.shared.account.business.ControllerException;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
@ -31,6 +32,7 @@ public class ApplicationTransactionJoinInterceptor {
@AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception {
try {
if (em == null) {
return context.proceed();
} else {
@ -42,6 +44,16 @@ public class ApplicationTransactionJoinInterceptor {
}
}
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;
}
}
}