Spring Boot无默认租户的多租户实现方式

3
我们正在努力解决这个问题!我们尝试在不使用默认租户的情况下为我们的Spring Boot服务使用MTA。这意味着当当前上下文中没有租户时,我们希望从我们的CurrentTenantIdentifierResolver实现中返回null。这在我们使用JavaEE + Hibernate + Deltaspike Data构建的其他服务中运行良好,但在启动Spring服务期间失败。
异常消息如下:“Caused by: org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified”
问题似乎是仓库工厂在启动时尝试创建我们的crud存储库的实例/bean时没有租户(也不会有):
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.(JpaRepositoryFactory.java:59) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
因此,我们的问题是,如果有人有解决方案,我们将避免使用默认租户。对我们来说,使用默认租户似乎容易出错,并且会隐藏非法应用程序状态。
我们使用的spring boot父版本为1.5.3.RELEASE。
如果重要:我们的服务是简单的JSON RPC服务,租户将存在于传入的http请求中,并由mvc拦截器将其设置在线程本地。在JavaEE世界中,我们使用webfilters进行此操作。
非常感谢您的帮助!
1个回答

0

其实我还没有尝试过这种方法。

最初异常是在代码中抛出的。

protected AbstractSessionImpl(SessionFactoryImpl factory, String tenantIdentifier) {
    this.factory = factory;
    this.tenantIdentifier = tenantIdentifier;
    if ( MultiTenancyStrategy.NONE == factory.getSettings().getMultiTenancyStrategy() ) {
        if ( tenantIdentifier != null ) {
            throw new HibernateException( "SessionFactory was not configured for multi-tenancy" );
        }
    }
    else {
        if ( tenantIdentifier == null ) {
            throw new HibernateException( "SessionFactory configured for multi-tenancy, but no tenant identifier specified" );
        }
    }
}

建议是将Hibernate会话工厂(参见示例)替换为在当前上下文中没有租户时返回MultiTenancyStrategy.NONE

1
嗯,这感觉不对。我们正在使用多租户架构,但如果没有租户存在,告诉Hibernate我们并没有使用它。这将再次引入隐藏非法状态的可能性... - undefined
1
目前最好的解决方案是返回一个空字符串,当连接被使用时会导致异常。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接