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 javax.sql.DataSource;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/**
* @author michael
*/
public class DataSourceManager { public class DataSourceManager {
private static DataSourceDecipher decipher; private static DataSourceDecipher decipher;

View File

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

View File

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