Skip to content

Commit

Permalink
Handle exception on inTransaction apply (#5700)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjav-desai authored Dec 15, 2022
1 parent 46d68a7 commit 600ac88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ public <U, T extends Subscribable<U>> T inTransaction(Function<DbTransaction, T>
dbMapperManager,
mapperManager);

T result = executor.apply(execute);
T result;

try {
result = executor.apply(execute);
} catch (RuntimeException e) {
execute.doRollback();
throw e;
}

if (result instanceof Multi) {
Multi<U> multi = (Multi<U>) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ void txErrorHandling() {
assertThat("Wrong exception propagated.", ((RuntimeException) result).getMessage(), is(equalTo(message)));
}

@Test
void txExceptionHandling() {
String message = "BOOM IN TX!!!";

JdbcDbClient dbClient = (JdbcDbClient) JdbcDbClientProviderBuilder.create()
.connectionPool(POOL)
.build();
try {
dbClient.inTransaction(tx -> {
throw new RuntimeException(message);
});
} catch (RuntimeException result) {
assertThat("Wrong exception propagated.", result.getMessage(), is(equalTo(message)));
return;
}
fail("Wrong (or no) exception propagated, expected RuntimeException!");
}

@Test
void txResultHandling() {
JdbcDbClient dbClient = (JdbcDbClient) JdbcDbClientProviderBuilder.create()
Expand Down

0 comments on commit 600ac88

Please sign in to comment.