Skip to content

Commit 0861b89

Browse files
committed
[#1514] Catch errors thrown while executing a transactions
Before, we were ignoring some errors if they were happening in the worker provided to the transactions. Because of this, the transaction wasn't always rollbacked or closed.
1 parent 2fe3e4a commit 0861b89

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinySessionImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ Uni<T> execute(Function<Mutiny.Transaction, Uni<T>> work) {
508508
* roll back) and an error thrown by the work.
509509
*/
510510
Uni<T> executeInTransaction(Function<Mutiny.Transaction, Uni<T>> work) {
511-
return work.apply( this )
511+
return Uni.createFrom()
512+
.deferred( () -> work.apply( this ) )
512513
// only flush() if the work completed with no exception
513514
.call( this::flush ).call( this::beforeCompletion )
514515
// in the case of an exception or cancellation

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Uni<T> execute(Function<Mutiny.Transaction, Uni<T>> work) {
267267
* and an error thrown by the work.
268268
*/
269269
Uni<T> executeInTransaction(Function<Mutiny.Transaction, Uni<T>> work) {
270-
return work.apply( this )
270+
return Uni.createFrom().deferred( () -> work.apply( this ) )
271271
// in the case of an exception or cancellation
272272
// we need to rollback the transaction
273273
.onFailure().call( this::rollback )

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageSessionImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import static org.hibernate.reactive.util.impl.CompletionStages.applyToAll;
4848
import static org.hibernate.reactive.util.impl.CompletionStages.returnOrRethrow;
49+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
4950

5051
/**
5152
* Implements the {@link Stage.Session} API. This delegating class is
@@ -434,7 +435,7 @@ CompletionStage<T> execute(Function<Stage.Transaction, CompletionStage<T>> work)
434435
* roll back) and an error thrown by the work.
435436
*/
436437
CompletionStage<T> executeInTransaction(Function<Stage.Transaction, CompletionStage<T>> work) {
437-
return work.apply( this )
438+
return voidFuture().thenCompose( v -> work.apply( this ) )
438439
// only flush() if the work completed with no exception
439440
.thenCompose( result -> flush().thenApply( v -> result ) )
440441
// have to capture the error here and pass it along,

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.function.Function;
2727

2828
import static org.hibernate.reactive.util.impl.CompletionStages.returnOrRethrow;
29+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
2930

3031
/**
3132
* Implements the {@link Stage.StatelessSession} API. This delegating
@@ -207,7 +208,8 @@ CompletionStage<T> execute(Function<Stage.Transaction, CompletionStage<T>> work)
207208
* and an error thrown by the work.
208209
*/
209210
CompletionStage<T> executeInTransaction(Function<Stage.Transaction, CompletionStage<T>> work) {
210-
return work.apply( this )
211+
return voidFuture()
212+
.thenCompose( v -> work.apply( this ) )
211213
// have to capture the error here and pass it along,
212214
// since we can't just return a CompletionStage that
213215
// rolls back the transaction from the handle() function

0 commit comments

Comments
 (0)