ensured transaction is rolled back in case a ControllerException occurs
This commit is contained in:
@ -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;
|
||||
|
||||
@ -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 {
|
||||
if (em.isJoinedToTransaction()) {
|
||||
LOGGER.trace("transaction already joined");
|
||||
} else {
|
||||
LOGGER.trace("joining transaction");
|
||||
em.joinTransaction();
|
||||
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;
|
||||
}
|
||||
return context.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,17 +32,28 @@ public class ApplicationTransactionJoinInterceptor {
|
||||
|
||||
@AroundInvoke
|
||||
public Object joinTransaction(InvocationContext context) throws Exception {
|
||||
if (em == null) {
|
||||
return context.proceed();
|
||||
} else {
|
||||
if (em.isJoinedToTransaction()) {
|
||||
LOGGER.trace("transaction already joined");
|
||||
try {
|
||||
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();
|
||||
} 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user