fix: 修复使用多数据源 + seata 分布式事务时,数据源解密错误的问题

This commit is contained in:
开源海哥 2023-08-08 18:37:52 +08:00
parent 02b7997d16
commit 9354f200c6
3 changed files with 27 additions and 7 deletions

View File

@ -21,6 +21,9 @@ import org.apache.ibatis.logging.LogFactory;
import javax.sql.DataSource;
import java.lang.reflect.Method;
/**
* @author michael
*/
public class DataSourceManager {
private static DataSourceDecipher decipher;

View File

@ -49,8 +49,13 @@ public class FlexDataSource extends AbstractDataSource {
private final DataSource defaultDataSource;
public FlexDataSource(String dataSourceKey, DataSource dataSource) {
this(dataSourceKey, dataSource, true);
}
DataSourceManager.decryptDataSource(dataSource);
public FlexDataSource(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource);
}
this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = dataSource;
@ -61,11 +66,19 @@ public class FlexDataSource extends AbstractDataSource {
}
public void addDataSource(String dataSourceKey, DataSource dataSource) {
DataSourceManager.decryptDataSource(dataSource);
addDataSource(dataSourceKey, dataSource, true);
}
public void addDataSource(String dataSourceKey, DataSource dataSource, boolean needDecryptDataSource) {
if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource);
}
dataSourceMap.put(dataSourceKey, dataSource);
dbTypeHashMap.put(dataSourceKey, DbTypeUtil.getDbType(dataSource));
}
public void removeDatasource(String dataSourceKey) {
dataSourceMap.remove(dataSourceKey);
dbTypeHashMap.remove(dataSourceKey);

View File

@ -81,18 +81,22 @@ public class MultiDataSourceAutoConfiguration {
DataSourceManager.setDecipher(dataSourceDecipher);
for (Map.Entry<String, Map<String, String>> entry : dataSourceProperties.entrySet()) {
DataSource dataSource = new DataSourceBuilder(entry.getValue()).build();
if (seataConfig !=null &&seataConfig.isEnable()){
if (seataConfig.getSeataMode() ==SeataMode.XA){
DataSourceManager.decryptDataSource(dataSource);
if (seataConfig != null && seataConfig.isEnable()) {
if (seataConfig.getSeataMode() == SeataMode.XA) {
dataSource = new DataSourceProxyXA(dataSource);
}else {
} else {
dataSource = new DataSourceProxy(dataSource);
}
}
if (flexDataSource == null) {
flexDataSource = new FlexDataSource(entry.getKey(), dataSource);
flexDataSource = new FlexDataSource(entry.getKey(), dataSource, false);
} else {
flexDataSource.addDataSource(entry.getKey(), dataSource);
flexDataSource.addDataSource(entry.getKey(), dataSource, false);
}
}
}