我需要创建一个具有在我的java代码(不基于用户请求)内部架构之间切换的能力的多元应用程序。手动更改多租户会话
我读过的文章: https://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/ http://www.greggbolinger.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();
}
的一点是,该连接不被切换,当手动设置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 connection to specified schema [" + tenantIdentifier + "]",e);
}
return connection;
}
}
是否可以强制切换会话?
是的,这是可能的,但需要在交易范围之外切换会话。 –
@Andriy Slobodyanyk,我不会手动创建交易。我有一个服务,没有事务性注释,并使库存。交易债券来自哪里? – Ermintar