Execution Context & Resolver

MongoTemplateResolver가 key별 MongoExecutionContext를 선택하고 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 naming

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

collection 이름이 애플리케이션 수명 동안 완전히 고정인 경우 DriverMongoExecutionContext.cachedCollectionNameResolver(...)를 사용할 수 있습니다. tenant/request/time에 따라 이름이 바뀌는 resolver에는 캐시 helper를 사용하지 않습니다.

Session scope와 transaction

getTxJob(...)에서 만든 ClientSession은 MongoExecutionContext#getSessionScope()가 같은 DSL 실행에만 전파됩니다. DriverMongoExecutionContext는 기본적으로 자신의 MongoClient를 session scope로 사용합니다.

Spring Data MongoDB를 사용하는 경우

© 2026 Byeolnaerim. All rights reserved.소개개인정보처리방침