Skip to content

Allow template.save() to find if it is in a transaction. #1758

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
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 @@ -80,7 +80,7 @@ public <T> Mono<T> save(T entity, String... scopeAndCollection) {

String scope = scopeAndCollection.length > 0 ? scopeAndCollection[0] : null;
String collection = scopeAndCollection.length > 1 ? scopeAndCollection[1] : null;
return Mono.defer(() -> {
return Mono.deferContextual( ctx1 -> {
final CouchbasePersistentEntity<?> mapperEntity = getConverter().getMappingContext()
.getPersistentEntity(entity.getClass());
final CouchbasePersistentProperty versionProperty = mapperEntity.getVersionProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertInTransaction;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertInReactiveTransaction;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInReactiveTransaction;

import org.springframework.data.couchbase.domain.PersonWithoutVersion;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -32,7 +32,6 @@
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.transactions.TransactionsConfig;
import org.springframework.data.couchbase.util.Capabilities;
import org.springframework.data.couchbase.util.ClusterType;
Expand All @@ -55,45 +54,53 @@ public class SDKTransactionsSaveIntegrationTests extends JavaIntegrationTests {
@BeforeEach
public void beforeEachTest() {
assertNotInTransaction();
assertNotInReactiveTransaction();
}

@AfterEach
public void afterEachTest() {
assertNotInTransaction();
assertNotInReactiveTransaction();
}


@DisplayName("ReactiveCouchbaseTemplate.save() called inside a reactive SDK transaction should work")
@Test
public void reactiveSaveInReactiveTransaction() {
couchbaseClientFactory.getCluster().reactive().transactions().run(ctx -> {
PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White");
return reactiveOps.save(p);
return reactiveOps.save(p).then(assertInReactiveTransaction());
}).block();
}

@DisplayName("ReactiveCouchbaseTemplate.save().block() called inside a non-reactive SDK transaction should work")
@Test
public void reactiveSaveInBlockingTransaction() {
couchbaseClientFactory.getCluster().transactions().run(ctx -> {
assertInTransaction();
PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White");
reactiveOps.save(p).block();
});
}

// This should not work because ops.save(p) calls block() so everything in that call
// does not have the reactive context (which has the transaction context)
// what happens is the ops.save(p) is not in a transaction. (it will call upsert instead of insert)
@DisplayName("ReactiveCouchbaseTemplate.save() called inside a reactive SDK transaction should work")
@Test
public void blockingSaveInReactiveTransaction() {
couchbaseClientFactory.getCluster().reactive().transactions().run(ctx -> {
PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White");
ops.save(p);
return Mono.empty();
return assertInReactiveTransaction();
}).block();
}

@DisplayName("ReactiveCouchbaseTemplate.save().block() called inside a non-reactive SDK transaction should work")
@Test
public void blockingSaveInBlockingTransaction() {
couchbaseClientFactory.getCluster().transactions().run(ctx -> {
assertInTransaction();
PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White");
ops.save(p);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.springframework.data.couchbase.core.TransactionalSupport;
import reactor.core.publisher.Mono;

/**
* Utility methods for transaction tests.
Expand All @@ -35,4 +36,16 @@ public static void assertInTransaction() {
public static void assertNotInTransaction() {
assertFalse(TransactionalSupport.checkForTransactionInThreadLocalStorage().block().isPresent());
}

public static <T> Mono<T> assertInReactiveTransaction(T... obj) {
return Mono.deferContextual((ctx1) ->
TransactionalSupport.checkForTransactionInThreadLocalStorage()
.flatMap(ctx2 -> ctx2.isPresent() ? (obj.length>0 ? Mono.just(obj[0]) : Mono.empty()) : Mono.error(new RuntimeException("in transaction"))));
}

public static <T> Mono<T> assertNotInReactiveTransaction(T... obj) {
return Mono.deferContextual((ctx1) ->
TransactionalSupport.checkForTransactionInThreadLocalStorage()
.flatMap(ctx2 -> !ctx2.isPresent() ? (obj.length>0 ? Mono.just(obj[0]) : Mono.empty()) : Mono.error(new RuntimeException("in transaction"))));
}
}