Skip to content

Commit 25a7a06

Browse files
authoredMay 22, 2024··
Transaction manager should catch Throwable (#2954)
1 parent 28b4804 commit 25a7a06

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed
 

‎data-tx/src/main/java/io/micronaut/transaction/async/AsyncUsingSyncTransactionOperations.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ public <T> CompletionStage<T> withTransaction(TransactionDefinition definition,
4747
Function<AsyncTransactionStatus<C>, CompletionStage<T>> handler) {
4848
CompletableFuture<T> newResult = new CompletableFuture<>();
4949
PropagatedContext propagatedContext = PropagatedContext.getOrEmpty();
50-
try (PropagatedContext.Scope scope = propagatedContext.propagate()) { // Propagate to clean up the scope
50+
try (PropagatedContext.Scope ignore = propagatedContext.propagate()) { // Propagate to clean up the scope
5151
TransactionStatus<C> status = synchronousTransactionManager.getTransaction(definition);
52-
PropagatedContext txPropagatedContext = PropagatedContext.get();
5352
CompletionStage<T> result;
5453
try {
5554
result = handler.apply(new DefaultAsyncTransactionStatus<>(status));
56-
} catch (Exception e) {
55+
} catch (Throwable e) {
5756
CompletableFuture<T> r = new CompletableFuture<>();
5857
r.completeExceptionally(e);
5958
result = r;

‎data-tx/src/main/java/io/micronaut/transaction/support/AbstractTransactionOperations.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private <R> R executeTransactional(T transaction, TransactionCallback<C, R> call
331331
R result;
332332
try {
333333
result = callback.apply(transaction);
334-
} catch (Exception e) {
334+
} catch (Throwable e) {
335335
if (definition.rollbackOn(e)) {
336336
rollbackInternal(transaction);
337337
} else {

‎doc-examples/jdbc-example-kotlin/src/main/kotlin/example/PersonSuspendRepositoryService.kt

+22
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ open class PersonSuspendRepositoryService(private val parentSuspendRepository: P
5959
throw RuntimeException("exception")
6060
}
6161

62+
@Transactional
63+
open fun normalStoreThrowable() {
64+
saveOne()
65+
throw Throwable("exception")
66+
}
67+
68+
@Transactional
69+
open fun normalThrowable() {
70+
throw Throwable("exception")
71+
}
72+
73+
@Transactional
74+
open suspend fun coroutinesStoreThrowable() {
75+
saveOneSuspended()
76+
throw Throwable("exception")
77+
}
78+
79+
@Transactional
80+
open suspend fun coroutinesThrowable() {
81+
throw Throwable("exception")
82+
}
83+
6284
@Transactional
6385
open suspend fun coroutinesStore() {
6486
saveOneSuspended()

‎doc-examples/jdbc-example-kotlin/src/test/kotlin/example/TxTest.kt

+52
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,56 @@ class TxTest {
154154
Assertions.assertEquals(0, service.countForCustomDb())
155155
}
156156

157+
@Test
158+
@Order(12)
159+
fun storeThrowable() {
160+
repeat(10000) { // Validate the connection is properly closed
161+
assertThrows<Throwable> {
162+
service.normalStoreThrowable()
163+
}
164+
165+
Assertions.assertEquals(0, service.count())
166+
}
167+
}
168+
169+
@Test
170+
@Order(13)
171+
fun throwable() {
172+
repeat(10000) { // Validate the connection is properly closed
173+
assertThrows<Throwable> {
174+
service.normalThrowable()
175+
}
176+
177+
Assertions.assertEquals(0, service.count())
178+
}
179+
}
180+
181+
@Test
182+
@Order(14)
183+
fun coroutineStoreThrowable() {
184+
runBlocking {
185+
repeat(10000) { // Validate the connection is properly closed
186+
assertThrows<Throwable> {
187+
service.coroutinesStoreThrowable()
188+
}
189+
190+
Assertions.assertEquals(0, service.count())
191+
}
192+
}
193+
}
194+
195+
@Test
196+
@Order(15)
197+
fun coroutineThrowable() {
198+
runBlocking {
199+
repeat(10000) { // Validate the connection is properly closed
200+
assertThrows<Throwable> {
201+
service.coroutinesThrowable()
202+
}
203+
204+
Assertions.assertEquals(0, service.count())
205+
}
206+
}
207+
}
208+
157209
}

0 commit comments

Comments
 (0)
Please sign in to comment.