Skip to content

Commit 0165fdb

Browse files
committed
Transaction Support.
1 parent b4a93d7 commit 0165fdb

File tree

5 files changed

+22
-50
lines changed

5 files changed

+22
-50
lines changed

.mvn/wrapper/maven-wrapper.jar

-47.2 KB
Binary file not shown.

src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java

+13-26
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,13 @@
1717

1818
import java.time.Duration;
1919
import java.time.temporal.ChronoUnit;
20-
import java.util.Optional;
21-
import java.util.UUID;
2220
import java.util.function.Supplier;
2321

24-
import com.couchbase.transactions.AttemptContext;
25-
import com.couchbase.transactions.AttemptContextReactiveAccessor;
26-
import com.couchbase.transactions.TransactionContext;
27-
import com.couchbase.transactions.config.MergedTransactionConfig;
28-
import com.couchbase.transactions.config.PerTransactionConfig;
29-
import com.couchbase.transactions.config.PerTransactionConfigBuilder;
3022
import org.springframework.dao.support.PersistenceExceptionTranslator;
3123
import org.springframework.data.couchbase.core.CouchbaseExceptionTranslator;
3224
import org.springframework.data.couchbase.transaction.ClientSession;
3325
import org.springframework.data.couchbase.transaction.ClientSessionImpl;
3426
import org.springframework.data.couchbase.transaction.ClientSessionOptions;
35-
import org.springframework.data.couchbase.transaction.CouchbaseStuffHandle;
3627

3728
import com.couchbase.client.core.env.Authenticator;
3829
import com.couchbase.client.core.env.OwnedSupplier;
@@ -43,7 +34,9 @@
4334
import com.couchbase.client.java.Collection;
4435
import com.couchbase.client.java.Scope;
4536
import com.couchbase.client.java.env.ClusterEnvironment;
37+
import com.couchbase.transactions.AttemptContext;
4638
import com.couchbase.transactions.AttemptContextReactive;
39+
import com.couchbase.transactions.AttemptContextReactiveAccessor;
4740
import com.couchbase.transactions.Transactions;
4841
import com.couchbase.transactions.config.TransactionConfig;
4942

@@ -85,16 +78,10 @@ public SimpleCouchbaseClientFactory(final Cluster cluster, final String bucketNa
8578

8679
private SimpleCouchbaseClientFactory(final Supplier<Cluster> cluster, final String bucketName,
8780
final String scopeName) {
88-
this(cluster, bucketName, scopeName, null);
89-
}
90-
91-
private SimpleCouchbaseClientFactory(final Supplier<Cluster> cluster, final String bucketName, final String scopeName,
92-
final CouchbaseStuffHandle transactionalOperator) {
9381
this.cluster = cluster;
9482
this.bucket = cluster.get().bucket(bucketName);
9583
this.scope = scopeName == null ? bucket.defaultScope() : bucket.scope(scopeName);
9684
this.exceptionTranslator = new CouchbaseExceptionTranslator();
97-
//this.transactionalOperator = transactionalOperator;
9885
}
9986

10087
@Override
@@ -142,23 +129,23 @@ public PersistenceExceptionTranslator getExceptionTranslator() {
142129
@Override
143130
public ClientSession getSession(ClientSessionOptions options, Transactions transactions, TransactionConfig config,
144131
AttemptContextReactive atr) {
145-
// can't we just use AttemptContextReactive everywhere? Instead of creating AttemptContext(atr), then
132+
// can't we just use AttemptContextReactive everywhere? Instead of creating AttemptContext(atr), then
146133
// accessing at.getACR() ?
147-
AttemptContext at = AttemptContextReactiveAccessor.from( atr != null ? atr : AttemptContextReactiveAccessor.newAttemptContextReactive(transactions.reactive()));
134+
AttemptContext at = AttemptContextReactiveAccessor
135+
.from(atr != null ? atr : AttemptContextReactiveAccessor.newAttemptContextReactive(transactions.reactive()));
148136

149137
return new ClientSessionImpl(this, transactions, config, at);
150138
}
151139

140+
// @Override
141+
// public CouchbaseClientFactory with(CouchbaseStuffHandle txOp) {
142+
// return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scope.name(), txOp);
143+
// }
152144

153-
//@Override
154-
//public CouchbaseClientFactory with(CouchbaseStuffHandle txOp) {
155-
// return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scope.name(), txOp);
156-
//}
157-
158-
//@Override
159-
//public CouchbaseStuffHandle getTransactionalOperator() {
160-
// return (CouchbaseStuffHandle) transactionalOperator;
161-
//}
145+
// @Override
146+
// public CouchbaseStuffHandle getTransactionalOperator() {
147+
// return (CouchbaseStuffHandle) transactionalOperator;
148+
// }
162149

163150
@Override
164151
public void close() {

src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionIntegrationTests.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public void replaceWithCasConflictResolvedViaRetry() {
377377

378378
AtomicInteger tryCount = new AtomicInteger(0);
379379
Mono<TransactionResult> result = transactions.reactive(ctx -> { // get the ctx
380-
// see TransactionalOperatorImpl.tranactional().
380+
// see TransactionalOperatorImpl.transactional().
381381
ClientSession clientSession = couchbaseClientFactory
382382
.getSession(ClientSessionOptions.builder().causallyConsistent(true).build(), transactions, null, ctx);
383383
ReactiveCouchbaseResourceHolder resourceHolder = new ReactiveCouchbaseResourceHolder(clientSession,
@@ -1009,12 +1009,6 @@ public Person declarativeFindReplacePersonCallback(Person person, AtomicInteger
10091009
System.err.println("declarativeFindReplacePersonCallback try: "+tryCount.incrementAndGet());
10101010
System.err.println("declarativeFindReplacePersonCallback cluster : "+callbackTm.template().getCouchbaseClientFactory().getCluster().block());
10111011
System.err.println("declarativeFindReplacePersonCallback resourceHolder : "+org.springframework.transaction.support.TransactionSynchronizationManager.getResource(callbackTm.template().getCouchbaseClientFactory().getCluster().block()));
1012-
/* what are we trying to see here???
1013-
TransactionSynchronizationManager.forCurrentTransaction().flatMap( sm -> {
1014-
System.err.println("declarativeFindReplacePersonCallback reactive resourceHolder : "+sm.getResource(callbackTm.template().getCouchbaseClientFactory().getCluster().block()));
1015-
return Mono.just(sm);
1016-
}).block();
1017-
*/
10181012
Person p = personOperations.findById(Person.class).one(person.getId().toString());
10191013
return personOperations.replaceById(Person.class).one(p);
10201014
}
@@ -1045,7 +1039,7 @@ public Mono<Person> declarativeFindReplacePersonReactive(Person person, AtomicIn
10451039
* @param person
10461040
* @return
10471041
*/
1048-
@Transactional(transactionManager = BeanNames.COUCHBASE_TRANSACTION_MANAGER)
1042+
@Transactional(transactionManager = BeanNames.COUCHBASE_TRANSACTION_MANAGER) // doesn't retry
10491043
public Person declarativeFindReplacePerson(Person person, AtomicInteger tryCount) {
10501044
assertInAnnotationTransaction(true);
10511045
System.err.println("declarativeFindReplacePerson try: "+tryCount.incrementAndGet());
@@ -1054,7 +1048,7 @@ public Person declarativeFindReplacePerson(Person person, AtomicInteger tryCount
10541048
}
10551049

10561050

1057-
@Transactional(transactionManager = BeanNames.REACTIVE_COUCHBASE_TRANSACTION_MANAGER)
1051+
@Transactional(transactionManager = BeanNames.REACTIVE_COUCHBASE_TRANSACTION_MANAGER) // doesn't retry
10581052
public Mono<Person> declarativeSavePersonReactive(Person person) {
10591053
assertInAnnotationTransaction(true);
10601054
return personOperationsRx.insertById(Person.class).one(person);

src/test/java/org/springframework/data/couchbase/util/ClusterAwareIntegrationTests.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,8 @@ public abstract class ClusterAwareIntegrationTests {
6262
@BeforeAll
6363
static void setup(TestClusterConfig config) {
6464
testClusterConfig = config;
65-
ClusterEnvironment env = config.seed() != null && config.seed().contains("cloud.couchbase.com")
66-
? ClusterEnvironment.builder()
67-
.securityConfig(SecurityConfig.trustManagerFactory(InsecureTrustManagerFactory.INSTANCE).enableTls(true))
68-
.build()
69-
: ClusterEnvironment.builder().build();
70-
String connectString = config.seed() != null && config.seed().contains("cloud.couchbase.com") ? config.seed()
71-
: connectionString();
65+
ClusterEnvironment env = ClusterEnvironment.builder().build();
66+
String connectString = connectionString();
7267
try (CouchbaseClientFactory couchbaseClientFactory = new SimpleCouchbaseClientFactory(connectString,
7368
authenticator(), bucketName(), null, env)) {
7469
couchbaseClientFactory.getCluster().queryIndexes().createPrimaryIndex(bucketName(), CreatePrimaryQueryIndexOptions
@@ -137,12 +132,8 @@ public static String connectionString() {
137132
protected static Set<SeedNode> seedNodes() {
138133
return config().nodes().stream()
139134
.map(cfg -> SeedNode.create(cfg.hostname(),
140-
Optional.ofNullable(config().seed() != null && config().seed().contains("cloud.couchbase.com")
141-
? cfg.ports().get(Services.KV_TLS)
142-
: cfg.ports().get(Services.KV)),
143-
Optional.ofNullable(config().seed() != null && config().seed().contains("cloud.couchbase.com")
144-
? cfg.ports().get(Services.MANAGER_TLS)
145-
: cfg.ports().get(Services.MANAGER))))
135+
Optional.ofNullable(cfg.ports().get(Services.KV)),
136+
Optional.ofNullable(cfg.ports().get(Services.MANAGER))))
146137
.collect(Collectors.toSet());
147138
}
148139

src/test/resources/integration.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# If set to false, it is assumed that the host is managing the cluster and
33
# as a result no containers or anything will be spun up.
44
# Options: containerized, mocked, unmanaged
5-
cluster.type=mocked
5+
cluster.type=unmanaged
66
# Default configs for both cases
77
cluster.adminUsername=Administrator
88
cluster.adminPassword=password
@@ -11,5 +11,5 @@ cluster.mocked.numNodes=1
1111
cluster.mocked.numReplicas=1
1212
# Entry point configuration if not managed
1313
# value of hostname and ns_server port
14-
cluster.unmanaged.seed=127.0.0.1:8091
14+
cluster.unmanaged.seed=10.144.220.101:8091
1515
cluster.unmanaged.numReplicas=0

0 commit comments

Comments
 (0)