mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
optimize FlexDataSource
This commit is contained in:
parent
bf809f6098
commit
ca8facf1df
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user