Skip to content

DATAMONGO-1061 - change in MongoTemplate for setting readPreference w… #386

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

Closed
Closed
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 @@ -806,9 +806,7 @@ protected void ensureNotIterable(Object o) {
* @param collection
*/
protected void prepareCollection(DBCollection collection) {
if (this.readPreference != null) {
collection.setReadPreference(readPreference);
}
collection.setReadPreference(readPreference);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
Expand Down Expand Up @@ -122,6 +123,9 @@ public class MongoTemplateTests {
.parse("2.8");

@Autowired MongoTemplate template;
@Autowired @Qualifier("mongoTemplateReadFromSecondary") MongoTemplate mongoTemplateReadFromSecondary;


@Autowired MongoDbFactory factory;

MongoTemplate mappingTemplate;
Expand Down Expand Up @@ -3442,6 +3446,23 @@ public void shouldFetchMapOfLazyReferencesCorrectly() {
assertThat(target.lazyDbRefAnnotatedMap.values(), contains(two, one));
}

/**
* @see DATAMONGO-1061
*/
@Test
public void multipleTemplates() throws Exception {

Person person = new Person("Bogdan Apetrei");
person.setAge(31);
template.insert(person);

mongoTemplateReadFromSecondary.find(new Query(Criteria.where("_id").is(person.getId())), Person.class);
assertThat(mongoTemplateReadFromSecondary.getDb().getCollection("person").getReadPreference(),is(ReadPreference.secondary()));

template.find(new Query(Criteria.where("_id").is(person.getId())), Person.class);
assertThat(template.getDb().getCollection("person").getReadPreference(), is(ReadPreference.primary()));
}

static class TypeWithNumbers {

@Id String id;
Expand Down
7 changes: 6 additions & 1 deletion spring-data-mongodb/src/test/resources/infrastructure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
<constructor-arg name="databaseName" value="database"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" primary="true">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

<bean id="mongoTemplateReadFromSecondary" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<property name="readPreference" value="#{T(com.mongodb.ReadPreference).secondary()}" />
</bean>

</beans>