我需要創建一個具有在我的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