Skip to content

Commit

Permalink
Transaction manager should catch Throwable (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed May 22, 2024
1 parent 28b4804 commit 25a7a06
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ public <T> CompletionStage<T> withTransaction(TransactionDefinition definition,
Function<AsyncTransactionStatus<C>, CompletionStage<T>> handler) {
CompletableFuture<T> newResult = new CompletableFuture<>();
PropagatedContext propagatedContext = PropagatedContext.getOrEmpty();
try (PropagatedContext.Scope scope = propagatedContext.propagate()) { // Propagate to clean up the scope
try (PropagatedContext.Scope ignore = propagatedContext.propagate()) { // Propagate to clean up the scope
TransactionStatus<C> status = synchronousTransactionManager.getTransaction(definition);
PropagatedContext txPropagatedContext = PropagatedContext.get();
CompletionStage<T> result;
try {
result = handler.apply(new DefaultAsyncTransactionStatus<>(status));
} catch (Exception e) {
} catch (Throwable e) {
CompletableFuture<T> r = new CompletableFuture<>();
r.completeExceptionally(e);
result = r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private <R> R executeTransactional(T transaction, TransactionCallback<C, R> call
R result;
try {
result = callback.apply(transaction);
} catch (Exception e) {
} catch (Throwable e) {
if (definition.rollbackOn(e)) {
rollbackInternal(transaction);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ open class PersonSuspendRepositoryService(private val parentSuspendRepository: P
throw RuntimeException("exception")
}

@Transactional
open fun normalStoreThrowable() {
saveOne()
throw Throwable("exception")
}

@Transactional
open fun normalThrowable() {
throw Throwable("exception")
}

@Transactional
open suspend fun coroutinesStoreThrowable() {
saveOneSuspended()
throw Throwable("exception")
}

@Transactional
open suspend fun coroutinesThrowable() {
throw Throwable("exception")
}

@Transactional
open suspend fun coroutinesStore() {
saveOneSuspended()
Expand Down
52 changes: 52 additions & 0 deletions doc-examples/jdbc-example-kotlin/src/test/kotlin/example/TxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,56 @@ class TxTest {
Assertions.assertEquals(0, service.countForCustomDb())
}

@Test
@Order(12)
fun storeThrowable() {
repeat(10000) { // Validate the connection is properly closed
assertThrows<Throwable> {
service.normalStoreThrowable()
}

Assertions.assertEquals(0, service.count())
}
}

@Test
@Order(13)
fun throwable() {
repeat(10000) { // Validate the connection is properly closed
assertThrows<Throwable> {
service.normalThrowable()
}

Assertions.assertEquals(0, service.count())
}
}

@Test
@Order(14)
fun coroutineStoreThrowable() {
runBlocking {
repeat(10000) { // Validate the connection is properly closed
assertThrows<Throwable> {
service.coroutinesStoreThrowable()
}

Assertions.assertEquals(0, service.count())
}
}
}

@Test
@Order(15)
fun coroutineThrowable() {
runBlocking {
repeat(10000) { // Validate the connection is properly closed
assertThrows<Throwable> {
service.coroutinesThrowable()
}

Assertions.assertEquals(0, service.count())
}
}
}

}

0 comments on commit 25a7a06

Please sign in to comment.