From 6c8cb9eb85f8a5f48d2c86e618ddb772e5c74149 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 15 Jul 2020 15:13:29 +0200 Subject: [PATCH] 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. --- .../core/convert/DefaultDbRefResolver.java | 13 +++++++----- .../data/mongodb/core/ClientSessionTests.java | 20 ++++++++++--------- .../PersonRepositoryTransactionalTests.java | 1 + 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java index f0ee5ba41..02c9e9c2d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java @@ -115,14 +115,16 @@ public class DefaultDbRefResolver implements DbRefResolver { @Override public Document fetch(DBRef dbRef) { + MongoCollection mongoCollection = getCollection(dbRef); + if (LOGGER.isTraceEnabled()) { LOGGER.trace("Fetching DBRef '{}' from {}.{}.", dbRef.getId(), - StringUtils.hasText(dbRef.getDatabaseName()) ? dbRef.getDatabaseName() : mongoDbFactory.getMongoDatabase().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 +155,16 @@ public class DefaultDbRefResolver implements DbRefResolver { } DBRef databaseSource = refs.iterator().next(); + MongoCollection mongoCollection = getCollection(databaseSource); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Bulk fetching DBRefs {} from {}.{}.", ids, StringUtils.hasText(databaseSource.getDatabaseName()) ? databaseSource.getDatabaseName() - : mongoDbFactory.getMongoDatabase().getName(), + : mongoCollection.getNamespace().getDatabaseName(), databaseSource.getCollectionName()); } - List result = getCollection(databaseSource) // + List result = mongoCollection // .find(new Document("_id", new Document("$in", ids))) // .into(new ArrayList<>()); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java index f0b660f2c..79c3d8df4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java @@ -42,24 +42,26 @@ import com.mongodb.client.ClientSession; import com.mongodb.client.MongoClient; /** + * Integration tests for {@link ClientSession} through {@link MongoTemplate#withSession(ClientSession)}. + * * @author Christoph Strobl * @author Mark Paluch */ @ExtendWith({ MongoClientExtension.class }) @EnableIfReplicaSetAvailable @EnableIfMongoServerVersion(isGreaterThanEqual = "4.0") -public class ClientSessionTests { +class ClientSessionTests { private static final String DB_NAME = "client-session-tests"; private static final String COLLECTION_NAME = "test"; private static final String REF_COLLECTION_NAME = "test-with-ref"; - static @ReplSetClient MongoClient mongoClient; + private static @ReplSetClient MongoClient mongoClient; - MongoTemplate template; + private MongoTemplate template; @BeforeEach - public void setUp() { + void setUp() { MongoTestUtils.createOrReplaceCollection(DB_NAME, COLLECTION_NAME, mongoClient); @@ -68,7 +70,7 @@ public class ClientSessionTests { } @Test // DATAMONGO-1880 - public void shouldApplyClientSession() { + void shouldApplyClientSession() { ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); @@ -85,7 +87,7 @@ public class ClientSessionTests { } @Test // DATAMONGO-2241 - public void shouldReuseConfiguredInfrastructure() { + void shouldReuseConfiguredInfrastructure() { ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); @@ -101,7 +103,7 @@ public class ClientSessionTests { } @Test // DATAMONGO-1920 - public void withCommittedTransaction() { + void withCommittedTransaction() { ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); @@ -126,7 +128,7 @@ public class ClientSessionTests { } @Test // DATAMONGO-1920 - public void withAbortedTransaction() { + void withAbortedTransaction() { ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); @@ -151,7 +153,7 @@ public class ClientSessionTests { } @Test // DATAMONGO-2490 - public void shouldBeAbleToReadDbRefDuringTransaction() { + void shouldBeAbleToReadDbRefDuringTransaction() { SomeDoc ref = new SomeDoc("ref-1", "da value"); WithDbRef source = new WithDbRef("source-1", "da source", ref); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java index e5c08cafc..7a893a0d9 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java @@ -116,6 +116,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);