Replace signal materialization in TransactionAspectSupport with usingWhen

We now use Flux.usingWhen() instead materialize/dematerialize operators
to reuse Reactor's resource closure.

Until usingWhen() accepts a BiFunction to consume error signals,
we need to map error signals outside of usingWhen which requires
re-wrapping of the ReactiveTransaction object.

Also, reuse the current TransactionContext to leave Transaction
creation/propagation entirely to ReactiveTransactionManager instead
of creating new TransactionContexts.
This commit is contained in:
Mark Paluch
2019-05-07 11:08:01 +02:00
committed by Juergen Hoeller
parent 28c5d7b586
commit 1d80cbea35
3 changed files with 32 additions and 20 deletions

View File

@@ -834,15 +834,18 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
Mono<Object> retVal = (Mono) invocation.proceedWithInvocation();
return retVal
.onErrorResume(ex -> completeTransactionAfterThrowing(it, ex).then(Mono.error(ex))).materialize()
.flatMap(signal -> {
if (signal.isOnComplete() || signal.isOnNext()) {
return commitTransactionAfterReturning(it).thenReturn(signal);
}
return Mono.just(signal);
}).dematerialize();
// Need re-wrapping of ReactiveTransaction until we get hold of the exception
// through usingWhen.
return Mono.<Object, ReactiveTransactionInfo>usingWhen(Mono.just(it), s -> {
try {
return (Mono) invocation.proceedWithInvocation();
}
catch (Throwable throwable) {
return Mono.error(throwable);
}
}, this::commitTransactionAfterReturning, s -> Mono.empty())
.onErrorResume(ex -> completeTransactionAfterThrowing(it, ex)
.then(Mono.error(ex)));
}
catch (Throwable ex) {
// target invocation exception
@@ -860,15 +863,19 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
Flux<Object> retVal = Flux.from(this.adapter.toPublisher(invocation.proceedWithInvocation()));
return retVal
.onErrorResume(ex -> completeTransactionAfterThrowing(it, ex).then(Mono.error(ex)))
.materialize().flatMap(signal -> {
if (signal.isOnComplete()) {
return commitTransactionAfterReturning(it).materialize();
}
return Mono.just(signal);
}).dematerialize();
// Need re-wrapping of ReactiveTransaction until we get hold of the exception
// through usingWhen.
return Flux.usingWhen(Mono.just(it), s -> {
try {
return this.adapter.toPublisher(
invocation.proceedWithInvocation());
}
catch (Throwable throwable) {
return Mono.error(throwable);
}
}, this::commitTransactionAfterReturning, s -> Mono.empty())
.onErrorResume(ex -> completeTransactionAfterThrowing(it, ex)
.then(Mono.error(ex)));
}
catch (Throwable ex) {
// target invocation exception

View File

@@ -88,7 +88,7 @@ public abstract class TransactionContextManager {
return context -> {
TransactionContextHolder holder = context.get(TransactionContextHolder.class);
if (holder.hasContext()) {
context.put(TransactionContext.class, holder.currentContext());
return context.put(TransactionContext.class, holder.currentContext());
}
return context.put(TransactionContext.class, holder.createContext());
};