diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java index b36879b7..8876edad 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java @@ -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); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java index c1a1767e..cc9028cf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MapperInvocationHandler.java @@ -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; } diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java index 54e98a2c..0e204ffb 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java @@ -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; }