feat(datasource): 添加动态事务管理器并支持获取主数据源
This commit is contained in:
parent
19df73ad40
commit
e73d02a853
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user