From ce73802a32ebf646852f4e302b9b28238b41f53f Mon Sep 17 00:00:00 2001 From: lukaseder Date: Thu, 14 Mar 2019 15:34:45 +0100 Subject: [PATCH] [#8413] Rollback should not be called when TransactionListener.commitEnd() throws exception --- .../java/org/jooq/impl/DefaultDSLContext.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index f38a47ebae..e6e8385fa0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -513,6 +513,7 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri DefaultTransactionContext ctx = new DefaultTransactionContext(configuration.derive()); TransactionProvider provider = ctx.configuration().transactionProvider(); TransactionListeners listeners = new TransactionListeners(ctx.configuration()); + boolean committed = false; try { try { @@ -528,6 +529,7 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri try { listeners.commitStart(ctx); provider.commit(ctx); + committed = true; } finally { listeners.commitEnd(ctx); @@ -536,31 +538,39 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri // [#6608] [#7167] Errors are no longer handled differently catch (Throwable cause) { - if (cause instanceof Exception) - ctx.cause((Exception) cause); - else - ctx.causeThrowable(cause); - listeners.rollbackStart(ctx); - try { - provider.rollback(ctx); + // [#8413] Avoid rollback logic if commit was successful (exception in commitEnd()) + if (!committed) { + if (cause instanceof Exception) + ctx.cause((Exception) cause); + else + ctx.causeThrowable(cause); + + listeners.rollbackStart(ctx); + try { + provider.rollback(ctx); + } + + // [#3718] Use reflection to support also JDBC 4.0 + catch (Exception suppress) { + + cause.addSuppressed(suppress); + + } + listeners.rollbackEnd(ctx); } - // [#3718] Use reflection to support also JDBC 4.0 - catch (Exception suppress) { - - cause.addSuppressed(suppress); - - } - listeners.rollbackEnd(ctx); - // [#6608] [#7167] Errors are no longer handled differently if (cause instanceof RuntimeException) throw (RuntimeException) cause; else if (cause instanceof Error) throw (Error) cause; else - throw new DataAccessException("Rollback caused", cause); + throw new DataAccessException(committed + ? "Exception after commit" + : "Rollback caused" + , cause + ); } return result;