DATAMONGO-2490 - Polishing.

Remove unnecessary code. Reuse session-associated collection when logging to avoid unqualified calls to MongoDbFactory.getMongoDatabase(). Create collection before transaction in test for compatibility with older MongoDB servers.

Original pull request: #875.
This commit is contained in:
Mark Paluch
2020-07-15 15:13:29 +02:00
parent be46540959
commit d9477501ae
3 changed files with 17 additions and 10 deletions

View File

@@ -34,6 +34,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
@@ -44,8 +45,8 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.ClientSessionException;
import org.springframework.data.mongodb.LazyLoadingException;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoDatabaseUtils;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable;
import org.springframework.objenesis.ObjenesisStd;
@@ -115,14 +116,16 @@ public class DefaultDbRefResolver implements DbRefResolver {
@Override
public Document fetch(DBRef dbRef) {
MongoCollection<Document> mongoCollection = getCollection(dbRef);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Fetching DBRef '{}' from {}.{}.", dbRef.getId(),
StringUtils.hasText(dbRef.getDatabaseName()) ? dbRef.getDatabaseName() : mongoDbFactory.getDb().getName(),
StringUtils.hasText(dbRef.getDatabaseName()) ? dbRef.getDatabaseName()
: mongoCollection.getNamespace().getDatabaseName(),
dbRef.getCollectionName());
}
StringUtils.hasText(dbRef.getDatabaseName());
return getCollection(dbRef).find(Filters.eq("_id", dbRef.getId())).first();
return mongoCollection.find(Filters.eq("_id", dbRef.getId())).first();
}
/*
@@ -153,15 +156,16 @@ public class DefaultDbRefResolver implements DbRefResolver {
}
DBRef databaseSource = refs.iterator().next();
MongoCollection<Document> mongoCollection = getCollection(databaseSource);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Bulk fetching DBRefs {} from {}.{}.", ids,
StringUtils.hasText(databaseSource.getDatabaseName()) ? databaseSource.getDatabaseName()
: mongoDbFactory.getDb().getName(),
: mongoCollection.getNamespace().getDatabaseName(),
databaseSource.getCollectionName());
}
List<Document> result = getCollection(databaseSource) //
List<Document> result = mongoCollection //
.find(new Document("_id", new Document("$in", ids))) //
.into(new ArrayList<>());

View File

@@ -45,6 +45,8 @@ import com.mongodb.client.MongoClient;
import com.mongodb.client.ClientSession;
/**
* Integration tests for {@link ClientSession} through {@link MongoTemplate#withSession(ClientSession)}.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
@@ -61,7 +63,7 @@ public class ClientSessionTests {
MongoClient client;
@Before
public void setUp() {
void setUp() {
client = MongoTestUtils.replSetClient();
@@ -72,7 +74,7 @@ public class ClientSessionTests {
}
@Test // DATAMONGO-1880
public void shouldApplyClientSession() {
void shouldApplyClientSession() {
ClientSession session = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
@@ -89,7 +91,7 @@ public class ClientSessionTests {
}
@Test // DATAMONGO-2241
public void shouldReuseConfiguredInfrastructure() {
void shouldReuseConfiguredInfrastructure() {
ClientSession session = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
@@ -162,7 +164,7 @@ public class ClientSessionTests {
SomeDoc ref = new SomeDoc("ref-1", "da value");
WithDbRef source = new WithDbRef("source-1", "da source", ref);
ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
ClientSession session = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
assertThat(session.getOperationTime()).isNull();

View File

@@ -111,6 +111,7 @@ public class PersonRepositoryTransactionalTests {
public void beforeTransaction() {
createOrReplaceCollection(DB_NAME, template.getCollectionName(Person.class), client);
createOrReplaceCollection(DB_NAME, template.getCollectionName(User.class), client);
durzo = new Person("Durzo", "Blint", 700);
kylar = new Person("Kylar", "Stern", 21);