增加错误捕捉且提供日志记录 保证数据源切面出现异常时提供日志

pull/503/head
MagicJson 2024-07-11 00:00:26 +08:00
parent 5bef005075
commit 66d828ce6d
1 changed files with 15 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package com.ruoyi.framework.aspectj; 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.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; 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.AnnotationUtils;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder; import java.util.Objects;
import com.ruoyi.common.utils.StringUtils;
/** /**
* *
@ -34,24 +34,27 @@ public class DataSourceAspect
} }
// 增加错误捕捉与日志记录
@Around("dsPointCut()") @Around("dsPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable public Object around(ProceedingJoinPoint point) throws Throwable
{ {
DataSource dataSource = getDataSource(point); DataSource dataSource = getDataSource(point);
if (StringUtils.isNotNull(dataSource)) if (Objects.nonNull(dataSource))
{ {
DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
} }
try {
try
{
return point.proceed(); return point.proceed();
} } catch (Throwable throwable) {
finally logger.error("Error during data source switch: {}", throwable.getMessage());
{ throw throwable;
// 销毁数据源 在执行方法之后 } finally {
DynamicDataSourceContextHolder.clearDataSourceType(); DynamicDataSourceContextHolder.clearDataSourceType();
if(logger.isInfoEnabled()){
logger.info("Cleared data source setting after method execution");
}
} }
} }