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 966504dfa6
commit 6c8cb9eb85
3 changed files with 20 additions and 14 deletions

View File

@@ -115,14 +115,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.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<Document> 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<Document> result = getCollection(databaseSource) //
List<Document> result = mongoCollection //
.find(new Document("_id", new Document("$in", ids))) //
.into(new ArrayList<>());

View File

@@ -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);

View File

@@ -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);