Execution Context & Resolver

MongoTemplateResolver จะเลือก MongoExecutionContext ตาม key และ DriverMongoExecutionContext จะอธิบายวิธีการให้ขอบเขต database, session, mapping.


สัญญา MongoTemplateResolver

MongoTemplateResolver.java
java
public interface MongoTemplateResolver<K> {
  MongoExecutionContext getTemplate(K key);
}

ReactiveMongoDsl<K>는 key마다 MongoExecutionContext를 가져옵니다. MongoExecutionContext가 MongoDatabase, ClientSession, collection naming, entity ↔ Document mapping과 save lifecycle hook의 경계를 담당합니다.

DriverMongoExecutionContext

java
MongoClient frontClient = MongoClients.create(frontConnectionString);
MongoClient backClient = MongoClients.create(backConnectionString);

MongoExecutionContext frontContext = new DriverMongoExecutionContext(
    frontClient,
    frontClient.getDatabase("front")
);

MongoExecutionContext backContext = new DriverMongoExecutionContext(
    backClient,
    backClient.getDatabase("back")
);

MongoTemplateResolver<MongoTemplateName> resolver = key ->
    key == MongoTemplateName.BACK ? backContext : frontContext;

기본 collection 이름은 entity simple class name을 decapitalize해서 결정합니다. 프로젝트의 실제 naming 규칙이 다르면 생성자에 collection-name resolver를 전달합니다.

การตั้งชื่อ Collection

java
MongoExecutionContext context = new DriverMongoExecutionContext(
    mongoClient,
    mongoDatabase,
    entityClass -> collectionNameResolver(entityClass)
);

หากชื่อ collection ถูกกำหนดอย่างถาวรตลอดอายุการใช้งานของแอปพลิเคชัน สามารถใช้ DriverMongoExecutionContext.cachedCollectionNameResolver(...) ได้ สำหรับ resolver ที่เปลี่ยนชื่อขึ้นอยู่กับ tenant/request/time จะไม่ใช้ cache helper.

ขอบเขต Session และ transaction

ClientSession ที่สร้างจาก getTxJob(...) จะถูกส่งต่อไปยัง MongoExecutionContext#getSessionScope() เฉพาะใน DSL execution เดียวกัน DriverMongoExecutionContext จะใช้ MongoClient ของตนเองเป็น session scope โดยพื้นฐาน.

เมื่อใช้ Spring Data MongoDB