Commit ff5b05fe authored by Stephane Nicoll's avatar Stephane Nicoll

Override default consistency for Couchbase

Spring Data Couchbase 2.0 sets the default consistency to "update-after"
which is good for performance reason but can be quite confusing. Since
the team has decided to switch to "read-your-own-writes" in 2.1, Spring
Boot already offers the improved default right now.

This commit exposes an additional property that can be used to change
the Couchbase's default consistency.

Closes gh-5159
parent 81fdc99f
......@@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
import org.springframework.data.couchbase.core.query.Consistency;
import org.springframework.data.couchbase.repository.support.IndexManager;
/**
......@@ -79,6 +80,11 @@ public class CouchbaseAutoConfiguration {
return this.properties.getBucket().getPassword();
}
@Override
protected Consistency getDefaultConsistency() {
return this.properties.getConsistency();
}
@Override
@ConditionalOnMissingBean(name = "couchbaseTemplate")
@Bean(name = "couchbaseTemplate")
......
......@@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.couchbase.core.query.Consistency;
/**
* Configuration properties for Couchbase.
......@@ -45,6 +46,11 @@ public class CouchbaseProperties {
private final Bucket bucket = new Bucket();
/**
* Consistency to apply by default on generated queries.
*/
private Consistency consistency = Consistency.READ_YOUR_OWN_WRITES;
public boolean isAutoIndex() {
return this.autoIndex;
}
......@@ -65,6 +71,14 @@ public class CouchbaseProperties {
return this.bucket;
}
public Consistency getConsistency() {
return this.consistency;
}
public void setConsistency(Consistency consistency) {
this.consistency = consistency;
}
static class Bucket {
/**
......
......@@ -34,6 +34,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
import org.springframework.data.couchbase.core.query.Consistency;
import org.springframework.data.couchbase.repository.support.IndexManager;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -43,6 +44,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link CouchbaseAutoConfiguration}
*
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
public class CouchbaseAutoConfigurationTests {
......@@ -107,6 +109,13 @@ public class CouchbaseAutoConfigurationTests {
assertThat(indexManager.isIgnoreN1qlSecondary()).isFalse();
}
@Test
public void changeConsistency() {
load(CouchbaseTestConfiguration.class, "spring.data.couchbase.consistency=eventually-consistent");
CouchbaseTestConfiguration configuration = this.context.getBean(CouchbaseTestConfiguration.class);
assertThat(configuration.getDefaultConsistency()).isEqualTo(Consistency.EVENTUALLY_CONSISTENT);
}
@Test
public void overrideCouchbaseOperations() {
load(CouchbaseTemplateConfiguration.class);
......
......@@ -493,6 +493,7 @@ content into your application; rather pick only the properties that you need.
spring.data.couchbase.bootstrap-hosts=localhost # Couchbase nodes (host or IP address) to bootstrap from.
spring.data.couchbase.bucket.name= # Name of the bucket to connect to.
spring.data.couchbase.bucket.password= # Password of the bucket.
spring.data.couchbase.consistency=read-your-own-writes # Consistency to apply by default on generated queries.
# ELASTICSEARCH ({sc-spring-boot-autoconfigure}/elasticsearch/ElasticsearchProperties.{sc-ext}[ElasticsearchProperties])
spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment