diff --git a/api/src/org/labkey/api/data/DbScope.java b/api/src/org/labkey/api/data/DbScope.java index 70f28bece57..502ff95c684 100644 --- a/api/src/org/labkey/api/data/DbScope.java +++ b/api/src/org/labkey/api/data/DbScope.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.Nullable; import org.junit.Assert; import org.junit.Test; +import org.labkey.api.action.ApiUsageException; import org.labkey.api.audit.TransactionAuditProvider; import org.labkey.api.cache.Cache; import org.labkey.api.data.ConnectionWrapper.Closer; @@ -2166,8 +2167,7 @@ public static void closeAllConnectionsForCurrentThread() } try { - LOG.warn("Forcing close of still-pending transaction object. Current stack is ", new Throwable()); - LOG.warn("Forcing close of still-pending transaction object started at ", t._creation); + LOG.warn("Forcing close of still-pending transaction object started at {}. Current stack is ", t._creation, new Throwable()); t.close(); } catch (ConnectionAlreadyReleasedException ignored) @@ -2715,16 +2715,17 @@ public void commit() conn.commit(); conn.setAutoCommit(true); LOG.debug("setAutoCommit(true)"); - if (null != _closeOnClose) - try { _closeOnClose.close(); } catch (Exception ignore) {} } finally { + if (null != _closeOnClose) + try { _closeOnClose.close(); } catch (Exception ignore) {} if (null != conn) conn.internalClose(); - } - popCurrentTransaction(); + // Make sure to pop whether we successfully committed or not + popCurrentTransaction(); + } CommitTaskOption.POSTCOMMIT.run(this); } @@ -3164,6 +3165,24 @@ public void testAutoCommitFailure() closeAllConnectionsForCurrentThread(); } + @Test + public void tesCommitTaskFailure() + { + String message = "Expected failure"; + try (Transaction t = getLabKeyScope().ensureTransaction()) + { + t.addCommitTask(() -> { throw new ApiUsageException("Expected failure"); }, CommitTaskOption.PRECOMMIT); + t.commit(); + fail("Shouldn't have gotten here, expected ApiUsageException"); + } + catch (ApiUsageException e) + { + assertEquals("Bad message", message, e.getMessage()); + } + assertFalse(getLabKeyScope().isTransactionActive()); + closeAllConnectionsForCurrentThread(); + } + @Test public void testLockReleasedException() {