1

春のデータmongodbでMULTI-TENANTプロジェクトを開始しています。この投稿を読んだ後、テナントごとのコレクションは、mongodb ではかなりまともなアプローチです。

では、Spring Data Mongo でこれを達成するにはどうすればよいでしょうか? どうやら、すぐに使用できるソリューションは提供されていませんが、determinCollectionName メソッドをオーバーライドすることで MongoTemplate を微調整できると思いましたが、目的のためにパブリックでも保護されたメソッドでもないと想定しています。

ただし、SimpleMongoDbFactory を拡張し、ここで提供されているヒントに従って LocalThread 変数を使用することで、テナントごとのアプローチを非常に簡単にセットアップできます。

したがって、質問は次のとおりです。

  1. コレクションとドメイン クラス名のマッピングをオーバーライドできる安全な方法はありますか? ps: これは実行時に発生するはずなので、@collection アノテーションはまったく役に立たないと思います。
  2. テナントごとの収集が不可能な場合、マルチ DB アプローチを採用すると、パフォーマンスとリソースにどの程度のペナルティが発生しますか?
4

1 に答える 1

0

MappingMongoEntityInformation を拡張して getCollectionName() をオーバーライドできます。リポジトリ操作は、操作ごとに getCollectionName() を呼び出します。tenantId は ThreadLocal になると想定しています

public class TenantThreadLocal extends ThreadLocal<String> {
    private final static TenantThreadLocal instance = new TenantThreadLocal();

    public static TenantThreadLocal instance() {
        return instance;
    }
}

そして、コンストラクターが省略されたオーバーライドされたクラス。

public class TenantMappingMongoEntityInformation<T, ID extends java.io.Serializable>  extends MappingMongoEntityInformation<T, ID> {

    @Override
    public String getCollectionName() {
        return TenantThreadLocal.instance().get() + super.getCollectionName();
    }
}

次に、リポジトリを作成します。

MongoPersistentEntity<YourObject> persistentEntity = 
    (MongoPersistentEntity<YourObject>) 
    mongoOperations.getConverter()
    .getMappingContext()
    .getPersistentEntity(YourObject.class);

MongoEntityInformation<YourObject, ObjectId> mongoEntityInformation =
    new MappingMongoEntityInformation<YourObject, ObjectId>(persistentEntity);

CrudRepository<YourObject, ObjectId> repository =
    new SimpleMongoRepository<YourObject, ObjectId>(mongoEntityInformation, mongoOperations);
于 2013-07-28T04:37:49.690 に答える