DATAMONGO-2241 - Retain MongoConverter using MongoTemplate.withSession(…).

We now reuse the MongoConverter instance that is associated with a MongoTemplate when using withSession(…) instead of creating a new converter instance.
In consequence, we're reusing PersistentEntity instances, EntityInstantiators and generated accessor classes.

Original Pull Request: #734
This commit is contained in:
Mark Paluch
2019-03-29 20:14:23 +01:00
committed by Christoph Strobl
parent c3857209d5
commit 847dfebce0
2 changed files with 17 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ import org.bson.codecs.Codec;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -273,8 +274,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
this.mongoDbFactory = dbFactory;
this.exceptionTranslator = that.exceptionTranslator;
this.sessionSynchronization = that.sessionSynchronization;
this.mongoConverter = that.mongoConverter instanceof MappingMongoConverter ? getDefaultMongoConverter(dbFactory)
: that.mongoConverter;
this.mongoConverter = that.mongoConverter;
this.queryMapper = that.queryMapper;
this.updateMapper = that.updateMapper;
this.schemaMapper = that.schemaMapper;

View File

@@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import com.mongodb.MongoClient;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -30,7 +29,9 @@ import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.mongodb.test.util.MongoVersion;
@@ -39,6 +40,7 @@ import org.springframework.data.mongodb.test.util.ReplicaSet;
import org.springframework.data.util.Version;
import com.mongodb.ClientSessionOptions;
import com.mongodb.MongoClient;
import com.mongodb.client.ClientSession;
/**
@@ -89,6 +91,18 @@ public class ClientSessionTests {
session.close();
}
@Test // DATAMONGO-2241
public void shouldReuseConfiguredConverter() {
ClientSession session = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
MongoConverter converter = template.getConverter();
MongoConverter sessionTemplateConverter = template.withSession(() -> session)
.execute(MongoOperations::getConverter);
assertThat(sessionTemplateConverter).isSameAs(converter);
}
@Test // DATAMONGO-1920
@MongoVersion(asOf = "3.7.3")
public void withCommittedTransaction() {