From 66d828ce6dddfecb421d45f87298ec927d0965eb Mon Sep 17 00:00:00 2001 From: MagicJson Date: Thu, 11 Jul 2024 00:00:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E6=8D=95?= =?UTF-8?q?=E6=8D=89=E4=B8=94=E6=8F=90=E4=BE=9B=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=20=E4=BF=9D=E8=AF=81=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E5=88=87=E9=9D=A2=E5=87=BA=E7=8E=B0=E5=BC=82=E5=B8=B8=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/aspectj/DataSourceAspect.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java index f72b8051b..c7799584e 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java @@ -1,6 +1,7 @@ package com.ruoyi.framework.aspectj; -import java.util.Objects; +import com.ruoyi.common.annotation.DataSource; +import com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @@ -11,9 +12,8 @@ import org.slf4j.LoggerFactory; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import com.ruoyi.common.annotation.DataSource; -import com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder; -import com.ruoyi.common.utils.StringUtils; + +import java.util.Objects; /** * 多数据源处理 @@ -34,24 +34,27 @@ public class DataSourceAspect } + // 增加错误捕捉与日志记录 @Around("dsPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { DataSource dataSource = getDataSource(point); - if (StringUtils.isNotNull(dataSource)) + if (Objects.nonNull(dataSource)) { DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); } - - try - { + try { return point.proceed(); - } - finally - { - // 销毁数据源 在执行方法之后 + } catch (Throwable throwable) { + logger.error("Error during data source switch: {}", throwable.getMessage()); + throw throwable; + } finally { DynamicDataSourceContextHolder.clearDataSourceType(); + if(logger.isInfoEnabled()){ + logger.info("Cleared data source setting after method execution"); + } + } }