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
|
* @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;
|
||||||
|
|||||||
@ -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;
|
||||||
@ -48,12 +49,28 @@ public class AccountTransactionJoinInterceptor {
|
|||||||
|
|
||||||
@AroundInvoke
|
@AroundInvoke
|
||||||
public Object joinTransaction(InvocationContext context) throws Exception {
|
public Object joinTransaction(InvocationContext context) throws Exception {
|
||||||
|
try {
|
||||||
|
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();
|
||||||
|
} catch (ControllerException ex) {
|
||||||
|
if (em.isJoinedToTransaction()) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
if (LOGGER.isTraceEnabled()) {
|
||||||
|
LOGGER.trace("Transaction rolled back");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,6 +32,7 @@ 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();
|
return context.proceed();
|
||||||
} else {
|
} else {
|
||||||
@ -42,6 +44,16 @@ public class ApplicationTransactionJoinInterceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return context.proceed();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user