Skip to content

Replace signal materialization in TransactionAspectSupport with usingWhen #22911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -834,15 +834,18 @@ public Object invokeWithinTransaction(Method method, @Nullable Class<?> targetCl
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
Expand All @@ -860,15 +863,19 @@ public Object invokeWithinTransaction(Method method, @Nullable Class<?> targetCl
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static Function<Context, Context> getOrCreateContext() {
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());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Fail.fail;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
Expand Down Expand Up @@ -321,6 +322,7 @@ public void cannotCommitTransaction() throws Exception {
when(rtm.getReactiveTransaction(txatt)).thenReturn(Mono.just(status));
UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
when(rtm.commit(status)).thenReturn(Mono.error(ex));
when(rtm.rollback(status)).thenReturn(Mono.empty());

DefaultTestBean tb = new DefaultTestBean();
TestBean itb = (TestBean) advised(tb, rtm, tas);
Expand All @@ -329,7 +331,10 @@ public void cannotCommitTransaction() throws Exception {

Mono.from(itb.setName(name))
.as(StepVerifier::create)
.expectError(UnexpectedRollbackException.class)
.consumeErrorWith(throwable -> {
assertEquals(RuntimeException.class, throwable.getClass());
assertEquals(ex, throwable.getCause());
})
.verify();

// Should have invoked target and changed name
Expand Down