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

View File

@ -31,7 +31,6 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class MapperInvocationHandler implements InvocationHandler { public class MapperInvocationHandler implements InvocationHandler {
private static final String NONE_KEY = "!NONE";
private final Object mapper; private final Object mapper;
private final FlexDataSource dataSource; private final FlexDataSource dataSource;
@ -51,9 +50,10 @@ public class MapperInvocationHandler implements InvocationHandler {
String dataSourceKey = DataSourceKey.get(); String dataSourceKey = DataSourceKey.get();
if (StringUtil.isBlank(dataSourceKey)) { if (StringUtil.isBlank(dataSourceKey)) {
String methodKey = getMethodDataSource(method, proxy); //通过 @UseDataSource 或者 @Table(dataSource) 去获取
if (!NONE_KEY.equals(methodKey)) { String configDataSourceKey = getConfigDataSourceKey(method, proxy);
dataSourceKey = methodKey; if (StringUtil.isNotBlank(configDataSourceKey)) {
dataSourceKey = configDataSourceKey;
DataSourceKey.use(dataSourceKey); DataSourceKey.use(dataSourceKey);
clearDsKey = true; 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); UseDataSource useDataSource = method.getAnnotation(UseDataSource.class);
if (useDataSource != null && StringUtil.isNotBlank(useDataSource.value())) { if (useDataSource != null && StringUtil.isNotBlank(useDataSource.value())) {
return 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 @ConditionalOnMissingBean
public DataSource dataSource() { public DataSource dataSource() {
FlexDataSource routingDataSource = null; FlexDataSource flexDataSource = null;
if (dataSourceProperties != null && !dataSourceProperties.isEmpty()) { if (dataSourceProperties != null && !dataSourceProperties.isEmpty()) {
for (String key : dataSourceProperties.keySet()) { for (String key : dataSourceProperties.keySet()) {
DataSource dataSource = new DataSourceBuilder(dataSourceProperties.get(key)).build(); DataSource dataSource = new DataSourceBuilder(dataSourceProperties.get(key)).build();
if (routingDataSource == null) { if (flexDataSource == null) {
routingDataSource = new FlexDataSource(key, dataSource); flexDataSource = new FlexDataSource(key, dataSource);
} else { } else {
routingDataSource.addDataSource(key, dataSource); flexDataSource.addDataSource(key, dataSource);
} }
} }
} }
return routingDataSource; return flexDataSource;
} }