Browse Source

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.
pull/878/head
Mark Paluch 6 years ago
parent
commit
6c8cb9eb85
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java
  2. 20
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java
  3. 1
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java

13
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

@ -115,14 +115,16 @@ public class DefaultDbRefResolver implements DbRefResolver { @@ -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 { @@ -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<>());

20
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ClientSessionTests.java

@ -42,24 +42,26 @@ import com.mongodb.client.ClientSession; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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);

1
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryTransactionalTests.java

@ -116,6 +116,7 @@ public class PersonRepositoryTransactionalTests { @@ -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);

Loading…
Cancel
Save