与Mongodb和Gorm的多租户

与Mongodb和Gorm的多租户,mongodb,grails,gorm,multi-tenant,Mongodb,Grails,Gorm,Multi Tenant,我试图在数据库级别使用多租户在mongodb中保存对象。但是,对象始终保存在默认数据库中 my application.yml development: grails: mongodb: tenantResolverClass: org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver url: mongod

我试图在
数据库
级别使用多租户在mongodb中保存对象。但是,对象始终保存在默认数据库中

my application.yml

development:
        grails:
          mongodb:
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver
            url: mongodb://localhost/test
            connections:
              1:
                url: mongodb://localhost/test1db
              2:
                url: mongodb://localhost/test2db
              options:
                maxWaitTime: 10000
我可以看到,所有连接(默认值1和2)都被很好地加载到
内存连接源.groovy
类中的
connectionSourceMap

在控制器函数和拦截器设置以下属性之前调用拦截器函数:

System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "1")
我原以为由于租户id已设置为“1”,并且在
connectionSourceMap
中存在针对键“1”的对象,所以现在对象将保存在test1db中。但是,当我尝试保存对象时,情况并非如此

Tenants.withCurrent {
                   domainObj.save(flush: flush, validate: false)
               }

我试图调试应用程序

MongoDataStore.java

@Override
    public <T1> T1 withNewSession(Serializable tenantId, Closure<T1> callable) {
        MongoDatastore mongoDatastore = getDatastoreForTenantId(tenantId);
        Session session = mongoDatastore.connect();
        try {
            DatastoreUtils.bindNewSession(session);
            return callable.call(session);
        }
        finally {
            DatastoreUtils.unbindSession(session);
        }
    }
但无法找到解决问题的方法

谢谢你的帮助和时间。感谢您的帮助


1:

能否提供一个在github上重现该问题的示例?
11.2.3. Multi Tenancy and the Session Factory
Note that if you reference the default SessionFactory or PlatformTransactionManager in your classes that are injected via Spring, these will not be tenant aware and will point directly to default data source.