feat(datasource): 添加动态事务管理器并支持获取主数据源

This commit is contained in:
Dftre 2025-02-17 21:55:21 +08:00
parent 19df73ad40
commit e73d02a853
2 changed files with 30 additions and 0 deletions

View File

@ -82,6 +82,10 @@ public class DataSourceManagement implements InitializingBean {
});
}
public DataSource getPrimaryDataSource(){
return targetDataSources.get(dataSourceProperties.getPrimary());
}
public DataSource getDataSource(String name) {
return targetDataSources.get(name);
}

View File

@ -0,0 +1,26 @@
package com.ruoyi.framework.datasource;
import java.util.Objects;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Component;
@Component
public class DynamicTransactionManager extends JdbcTransactionManager {
@Autowired
DataSourceManagement dataSourceManagement;
@Override
public DataSource getDataSource() {
DataSource dataSource = dataSourceManagement.getDataSource(DynamicDataSourceContextHolder.getDataSourceType());
if (!Objects.isNull(dataSource)) {
return dataSource;
} else {
return dataSourceManagement.getPrimaryDataSource();
}
}
}