相关疑难解决方法(0)

手动更改多租户会话

我需要创建一个多租户应用程序,能够在我的 java 代码中的模式之间切换(不基于用户请求)。

我读过文章: https ://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/ http://www.greggbolinger。 当架构在 Rest-request 中传递时,com/tenant-per-schema-with-spring-boot/解决方案工作正常。

但是我需要实现以下逻辑:

public void compare(String originalSchema, String secondSchema){
    TenantContext.setCurrentTenant(originalSchema);
    List<MyObject> originalData = myRepository.findData();

    TenantContext.setCurrentTenant(secondSchema);
    List<MyObject> migratedData = myRepository.findData();
}
Run Code Online (Sandbox Code Playgroud)

关键是,当我手动设置 TenenantContext 时,该连接不会切换。MultiTenantConnectionProviderImpl.getConnection 仅在第一次调用我的存储库时调用。

 @Component
 public class MultiTenantConnectionProviderImpl implements  MultiTenantConnectionProvider {

     @Override
     public Connection getConnection(String tenantIdentifier) throws SQLException {
          final Connection connection = getAnyConnection();
          try {
               connection.createStatement().execute( "ALTER SESSION SET CURRENT_SCHEMA = " + tenantIdentifier );
          }
          catch ( SQLException e ) {
              throw new HibernateException(
      "Could not alter JDBC …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-data-jpa spring-boot

5
推荐指数
1
解决办法
1631
查看次数

标签 统计

hibernate ×1

spring ×1

spring-boot ×1

spring-data-jpa ×1