optimize FlexDataSource

This commit is contained in:
开源海哥 2023-04-09 11:55:04 +08:00
parent bf809f6098
commit ca8facf1df
3 changed files with 19 additions and 24 deletions

View File

@ -66,13 +66,11 @@ public class FlexDataSource extends AbstractDataSource {
}
Connection connection = TransactionalManager.getConnection(xid, dataSourceKey);
if (connection != null) {
return connection;
} else {
if (connection == null) {
connection = proxy(getDataSource().getConnection(), xid);
TransactionalManager.hold(xid, dataSourceKey, connection);
return connection;
}
return connection;
} else {
return getDataSource().getConnection();
}
@ -88,13 +86,11 @@ public class FlexDataSource extends AbstractDataSource {
dataSourceKey = defaultDataSourceKey;
}
Connection connection = TransactionalManager.getConnection(xid, dataSourceKey);
if (connection != null) {
return connection;
} else {
if (connection == null) {
connection = proxy(getDataSource().getConnection(username, password), xid);
TransactionalManager.hold(xid, dataSourceKey, connection);
return connection;
}
return connection;
} else {
return getDataSource().getConnection(username, password);
}
@ -137,9 +133,9 @@ public class FlexDataSource extends AbstractDataSource {
}
private static class ConnectionHandler implements InvocationHandler {
private static String[] proxyMethods = new String[]{"commit", "rollback", "close",};
private Connection original;
private String xid;
private static final String[] proxyMethods = new String[]{"commit", "rollback", "close",};
private final Connection original;
private final String xid;
public ConnectionHandler(Connection original, String xid) {
this.original = original;
@ -150,8 +146,7 @@ public class FlexDataSource extends AbstractDataSource {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (ArrayUtil.contains(proxyMethods, method.getName())
&& isTransactional()) {
//do nothing
return null;
return null; //do nothing
}
return method.invoke(original, args);
}

View File

@ -31,7 +31,6 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MapperInvocationHandler implements InvocationHandler {
private static final String NONE_KEY = "!NONE";
private final Object mapper;
private final FlexDataSource dataSource;
@ -51,9 +50,10 @@ public class MapperInvocationHandler implements InvocationHandler {
String dataSourceKey = DataSourceKey.get();
if (StringUtil.isBlank(dataSourceKey)) {
String methodKey = getMethodDataSource(method, proxy);
if (!NONE_KEY.equals(methodKey)) {
dataSourceKey = methodKey;
//通过 @UseDataSource 或者 @Table(dataSource) 去获取
String configDataSourceKey = getConfigDataSourceKey(method, proxy);
if (StringUtil.isNotBlank(configDataSourceKey)) {
dataSourceKey = configDataSourceKey;
DataSourceKey.use(dataSourceKey);
clearDsKey = true;
}
@ -83,7 +83,7 @@ public class MapperInvocationHandler implements InvocationHandler {
}
private static String getMethodDataSource(Method method, Object proxy) {
private static String getConfigDataSourceKey(Method method, Object proxy) {
UseDataSource useDataSource = method.getAnnotation(UseDataSource.class);
if (useDataSource != null && StringUtil.isNotBlank(useDataSource.value())) {
return useDataSource.value();
@ -99,7 +99,7 @@ public class MapperInvocationHandler implements InvocationHandler {
}
}
}
return NONE_KEY;
return null;
}

View File

@ -52,20 +52,20 @@ public class MultiDataSourceAutoConfiguration {
@ConditionalOnMissingBean
public DataSource dataSource() {
FlexDataSource routingDataSource = null;
FlexDataSource flexDataSource = null;
if (dataSourceProperties != null && !dataSourceProperties.isEmpty()) {
for (String key : dataSourceProperties.keySet()) {
DataSource dataSource = new DataSourceBuilder(dataSourceProperties.get(key)).build();
if (routingDataSource == null) {
routingDataSource = new FlexDataSource(key, dataSource);
if (flexDataSource == null) {
flexDataSource = new FlexDataSource(key, dataSource);
} else {
routingDataSource.addDataSource(key, dataSource);
flexDataSource.addDataSource(key, dataSource);
}
}
}
return routingDataSource;
return flexDataSource;
}