Skip to content

Commit

Permalink
Handle.inTransaction: improve exception when isolation restore fails
Browse files Browse the repository at this point in the history
Fixes #2343
  • Loading branch information
stevenschlansker committed May 4, 2023
1 parent 9c4fa9c commit 0babcb4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unreleased
- Handle.inTransaction: improve exception thrown when restoring transaction isolation #2343

# 3.38.2
- spring5 JdbiUtil: fix thread safety #2341
Expand Down
17 changes: 12 additions & 5 deletions core/src/main/java/org/jdbi/v3/core/Handle.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,22 @@ public <R, X extends Exception> 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 <code>callback</code> in a transaction.
* <p>
Expand Down

0 comments on commit 0babcb4

Please sign in to comment.