From 0babcb4cb8a2c5ec137382b22d8202477c5fb9e9 Mon Sep 17 00:00:00 2001 From: Steven Schlansker Date: Thu, 4 May 2023 14:42:46 -0700 Subject: [PATCH] Handle.inTransaction: improve exception when isolation restore fails Fixes #2343 --- RELEASE_NOTES.md | 1 + core/src/main/java/org/jdbi/v3/core/Handle.java | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 75a0ff7392..a2896f2b6e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,4 +1,5 @@ # Unreleased + - Handle.inTransaction: improve exception thrown when restoring transaction isolation #2343 # 3.38.2 - spring5 JdbiUtil: fix thread safety #2341 diff --git a/core/src/main/java/org/jdbi/v3/core/Handle.java b/core/src/main/java/org/jdbi/v3/core/Handle.java index 0c81112c17..611468feda 100644 --- a/core/src/main/java/org/jdbi/v3/core/Handle.java +++ b/core/src/main/java/org/jdbi/v3/core/Handle.java @@ -761,15 +761,22 @@ public R inTransaction(TransactionIsolationLevel level, return callback.withHandle(this); } - TransactionIsolationLevel currentLevel = getTransactionIsolationLevel(); - try { - setTransactionIsolationLevel(level); + try (SetTransactionIsolation isolation = new SetTransactionIsolation(level)) { return transactionHandler.inTransaction(this, level, callback); - } finally { - setTransactionIsolationLevel(currentLevel); } } + class SetTransactionIsolation implements AutoCloseable { + private final TransactionIsolationLevel prevLevel; + SetTransactionIsolation(TransactionIsolationLevel setLevel) { + prevLevel = getTransactionIsolationLevel(); + setTransactionIsolationLevel(setLevel); + } + @Override + public void close() { + setTransactionIsolationLevel(prevLevel); + } + } /** * Executes callback in a transaction. *