与Mongodb和Gorm的多租户

时间:2016-10-24 17:02:57

标签: mongodb grails gorm multi-tenant

我正在尝试使用DATABASE级别的多租户保存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)都很好地加载到connectionSourceMap类中的InMemoryConnectionSources.groovy

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

System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "1")

我一直在期待,因为租户ID已被设置为" 1"并反对关键" 1"存在于connectionSourceMap中,因此现在对象将保存在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);
        }
    }

mongoDataStore对象似乎包含正确的信息,默认数据库字符串设置为test1db。但是,会话对象再次将测试db称为默认db。我无法理解为什么会这样,我怎么能解决这个问题。

我觉得这个问题可能与文档[1]中的以下几点有关:

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.

但无法弄清楚如何解决这个问题。

感谢您的帮助和时间。任何帮助表示赞赏。

1:http://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy

0 个答案:

没有答案
相关问题