且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring Boot +具有多租户的Spring数据

更新时间:2022-10-20 08:55:38

未定义的JPA / Hibernate的任何属性可以使用 spring.jpa.properties 属性 application.properties



您链接的示例有3个多租户的属性:

 < prop key =hibernate.multiTenancy> SCHEMA&LT; /丙&GT; 
< prop key =hibernate.tenant_identifier_resolver> com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver< / prop>
< prop key =hibernate.multi_tenant_connection_provider> com.webapp.persistence.utility.MultiTenantContextConnectionProvider< / prop>

转换为Spring Boot的应用程序将包含 application.properties中的以下属性 file。

  spring.jpa.properties.hibernate.multiTenancy = SCHEMA 
spring。 jpa.properties.hibernate.tenant_identifier_resolver = com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.properties.hibernate.multi_tenant_connection_provider = com.webapp.persistence.utility.MultiTenantContextConnectionProvider
$对于你的情况(如你的问题所述)。

  p $ p 

spring.jpa.properties.hibernate.multiTenancy = DATABASE
spring.jpa.properties.hibernate.tenant_identifier_resolver = com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver
spring.jpa.properties.hibernate.multi_tenant_connection_provider = com的。 mystuff.MyMultiTenantConnectionProviderImplX

它不适用于Spring管理bean,因为hibernate控制着生命周期清除这些实例。

更多属性请参阅Spring Boot 参考指南


Is it possible to configure Spring Boot to use a MultiTenantConnectionProvider so that each client of my system connects to their own private database?

Specifically I am looking to use the built-in hibernate support for multi-tenancy:

And this is an example of the sort of config I am after, but I can't figure out how to use this in a Spring Boot setup:

I've tried adding these properties to application.properties:

spring.jpa.hibernate.multiTenancy=DATABASE
spring.jpa.hibernate.tenant_identifier_resolver=com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.hibernate.multi_tenant_connection_provider=com.mystuff.MyMultiTenantConnectionProviderImplX

I've also tried coding up my own CurrentTenantIdentifierResolver and MultiTenantConnectionProvider and tried serving these up from my main @Configuration bean:

@Bean
public CurrentTenantIdentifierResolver currentTenantIdentifierResolver() {
    return new CurrentTenantIdentifierResolver() {
        public String resolveCurrentTenantIdentifier() {
            // this is never called ...
        }
        public boolean validateExistingCurrentSessions() {
            // this is never called ...
        }
    };
}

@Bean
public MultiTenantConnectionProvider multiTenantConnectionProvider() {
    return new AbstractMultiTenantConnectionProvider() {
        protected ConnectionProvider getAnyConnectionProvider() {
            // this is never called ...
        }
        protected ConnectionProvider selectConnectionProvider(String s) {
            // this is never called ...
        }
    };
}

None of this seems to have any affect so my question is really how to get spring-boot / spring-data to use these multi-tenant classes?

Thanks for your help!

Any property for JPA/Hibernate that isn't defined can be set using the spring.jpa.properties property in the application.properties.

The sample you link to has 3 properties for multitenancy:

<prop key="hibernate.multiTenancy">SCHEMA</prop>
<prop key="hibernate.tenant_identifier_resolver">com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.webapp.persistence.utility.MultiTenantContextConnectionProvider</prop>

That converted to Spring Boot would be the following properties in the application.properties file.

spring.jpa.properties.hibernate.multiTenancy=SCHEMA
spring.jpa.properties.hibernate.tenant_identifier_resolver=com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.properties.hibernate.multi_tenant_connection_provider=com.webapp.persistence.utility.MultiTenantContextConnectionProvider

For your situation (as stated in your question).

spring.jpa.properties.hibernate.multiTenancy=DATABASE
spring.jpa.properties.hibernate.tenant_identifier_resolver=com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver 
spring.jpa.properties.hibernate.multi_tenant_connection_provider=com.mystuff.MyMultiTenantConnectionProviderImplX

It will not work with Spring manged beans as hibernate controls the lifecycle of those instances.

For more properties see the the Spring Boot reference guide.