From 28edce8542e20886c2ee552e6d318fe538a7ea14 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Tue, 30 Jan 2018 09:54:36 +0100 Subject: [PATCH] [WFLY-10146] Simplify handleExceptionInOurTx --- .../jboss/as/ejb3/tx/CMTTxInterceptor.java | 55 +++++++++---------- .../as/ejb3/tx/TimerCMTTxInterceptor.java | 4 +- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/ejb3/src/main/java/org/jboss/as/ejb3/tx/CMTTxInterceptor.java b/ejb3/src/main/java/org/jboss/as/ejb3/tx/CMTTxInterceptor.java index 052cf99e3a8b..ceaecaba9e45 100644 --- a/ejb3/src/main/java/org/jboss/as/ejb3/tx/CMTTxInterceptor.java +++ b/ejb3/src/main/java/org/jboss/as/ejb3/tx/CMTTxInterceptor.java @@ -63,8 +63,6 @@ */ public class CMTTxInterceptor implements Interceptor { - private static final int MAX_RETRIES = 5; - public static final InterceptorFactory FACTORY = new ImmediateInterceptorFactory(new CMTTxInterceptor()); @@ -119,29 +117,29 @@ protected void endTransaction(final Transaction tx) { } } - public void handleExceptionInOurTx(InterceptorContext invocation, Throwable t, Transaction tx, final EJBComponent component) throws Exception { + public Exception handleExceptionInOurTx(InterceptorContext invocation, Throwable t, Transaction tx, final EJBComponent component) { ApplicationExceptionDetails ae = component.getApplicationException(t.getClass(), invocation.getMethod()); if (ae != null) { if (ae.isRollback()) setRollbackOnly(tx); - throw (Exception) t; + return (Exception) t; } - - // if it's neither EJBException nor RemoteException - if (!(t instanceof EJBException || t instanceof RemoteException)) { - // errors and unchecked are wrapped into EJBException - if (t instanceof Error) { - //t = new EJBException(formatException("Unexpected Error", t)); - t = EjbLogger.ROOT_LOGGER.unexpectedError(t); - } else if (t instanceof RuntimeException) { - t = new EJBException((Exception) t); - } else { - // an application exception - throw (Exception) t; - } + try { + throw t; + } catch (EJBException | RemoteException e) { + setRollbackOnly(tx); + return e; + } catch (RuntimeException e) { + setRollbackOnly(tx); + return new EJBException(e); + } catch (Error e) { + setRollbackOnly(tx); + return EjbLogger.ROOT_LOGGER.unexpectedError(e); + } catch (Exception e) { + // an application exception + return e; + } catch (Throwable e) { + throw new EJBException(new UndeclaredThrowableException(e)); } - - setRollbackOnly(tx); - throw (Exception) t; } public Object processInvocation(InterceptorContext invocation) throws Exception { @@ -219,17 +217,14 @@ protected Object invokeInNoTx(InterceptorContext invocation, final EJBComponent } protected Object invokeInOurTx(InterceptorContext invocation, final EJBComponent component) throws Exception { - for (int i = 0; i < MAX_RETRIES; i++) { - Transaction tx = beginTransaction(); - try { - return invocation.proceed(); - } catch (Throwable t) { - handleExceptionInOurTx(invocation, t, tx, component); - } finally { - endTransaction(tx); - } + Transaction tx = beginTransaction(); + try { + return invocation.proceed(); + } catch (Throwable t) { + throw handleExceptionInOurTx(invocation, t, tx, component); + } finally { + endTransaction(tx); } - throw new RuntimeException("UNREACHABLE"); } protected Transaction beginTransaction() throws NotSupportedException, SystemException { diff --git a/ejb3/src/main/java/org/jboss/as/ejb3/tx/TimerCMTTxInterceptor.java b/ejb3/src/main/java/org/jboss/as/ejb3/tx/TimerCMTTxInterceptor.java index 4082bcf47dd1..57cf07528312 100644 --- a/ejb3/src/main/java/org/jboss/as/ejb3/tx/TimerCMTTxInterceptor.java +++ b/ejb3/src/main/java/org/jboss/as/ejb3/tx/TimerCMTTxInterceptor.java @@ -47,9 +47,9 @@ public class TimerCMTTxInterceptor extends CMTTxInterceptor { public static final InterceptorFactory FACTORY = new ImmediateInterceptorFactory(new TimerCMTTxInterceptor()); @Override - public void handleExceptionInOurTx(final InterceptorContext invocation, final Throwable t, final Transaction tx, final EJBComponent component) throws Exception { + public Exception handleExceptionInOurTx(final InterceptorContext invocation, final Throwable t, final Transaction tx, final EJBComponent component) { EXCEPTION.set(t); - super.handleExceptionInOurTx(invocation, t, tx, component); + return super.handleExceptionInOurTx(invocation, t, tx, component); } @Override