refactor: optimize FlexMapperProxy.java

This commit is contained in:
Michael Yang 2024-06-02 12:19:04 +08:00
parent 6f6011f406
commit 476e33db42

View File

@ -60,10 +60,6 @@ public class FlexMapperProxy<T> extends MybatisMapperProxy<T> {
//最终使用的数据源 //最终使用的数据源
String finalDsKey = userDsKey; String finalDsKey = userDsKey;
//由用户指定的数据类型
DbType userDbType = DialectFactory.getHintDbType();
DbType finalDbType = userDbType;
try { try {
if (StringUtil.isBlank(finalDsKey)) { if (StringUtil.isBlank(finalDsKey)) {
finalDsKey = getMethodDsKey(method, proxy); finalDsKey = getMethodDsKey(method, proxy);
@ -73,34 +69,29 @@ public class FlexMapperProxy<T> extends MybatisMapperProxy<T> {
finalDsKey = DataSourceKey.getShardingDsKey(finalDsKey, proxy, method, args); finalDsKey = DataSourceKey.getShardingDsKey(finalDsKey, proxy, method, args);
if (StringUtil.isNotBlank(finalDsKey) && !finalDsKey.equals(userDsKey)) { if (StringUtil.isNotBlank(finalDsKey) && !finalDsKey.equals(userDsKey)) {
DataSourceKey.use(finalDsKey);
needClearDsKey = true; needClearDsKey = true;
DataSourceKey.use(finalDsKey);
} }
DbType hintDbType = DialectFactory.getHintDbType();
if (finalDbType == null) { if (hintDbType == null) {
if (finalDsKey != null && dataSource != null) { if (finalDsKey != null && dataSource != null) {
//使用最终分片获取数据源类型 hintDbType = dataSource.getDbType(finalDsKey);
finalDbType = dataSource.getDbType(finalDsKey);
} }
if (finalDbType == null) { if (hintDbType == null) {
finalDbType = FlexGlobalConfig.getDefaultConfig().getDbType(); hintDbType = FlexGlobalConfig.getDefaultConfig().getDbType();
} }
needClearDbType = true; needClearDbType = true;
DialectFactory.setHintDbType(finalDbType); DialectFactory.setHintDbType(hintDbType);
} }
return cachedInvoker(method).invoke(proxy, method, args, sqlSession); return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
} catch (Throwable e) { } catch (Throwable e) {
throw ExceptionUtil.unwrapThrowable(e); throw ExceptionUtil.unwrapThrowable(e);
} finally { } finally {
if (needClearDbType) { if (needClearDbType) {
if (userDbType != null) { DialectFactory.clearHintDbType();
DialectFactory.setHintDbType(userDbType);
} else {
DialectFactory.clearHintDbType();
}
} }
if (needClearDsKey) { if (needClearDsKey) {
if (userDsKey != null) { if (userDsKey != null) {