db源上下文类
public class DbSourceContext {
private static final ThreadLocal<String> dbContext = new ThreadLocal<>();
public static void setDbSource(String source) {
// log.debug("set source ====> {}", source);
dbContext.set(source);
}
public static String getDbSource() {
// log.debug("get source ====> {}", dbContext.get());
return dbContext.get();
}
public static void clearDbSource() {
dbContext.remove();
}
}
数据源方面类
package com.pwc.digital.rc.config.datasource;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.pwc.digital.rc.common.util.SubjectUtil;
@Aspect
@Order(-1) // the execution of this aspect should be prior to @Transactional
@Component
public class DynamicDataSourceAspect {
@Autowired
private SecurityManager securityManager;
/**
* switch data source
*
* @param point
*/
@SuppressWarnings({"unchecked"})
@Before("execution(* com.pwc.digital.rc..*Mapper.*(..))")
public void switchDataSource(JoinPoint point) {
try {
DBSource dbSource = (DBSource) point.getSignature().getDeclaringType().getAnnotation(DBSource.class);
if (dbSource != null) {
if (dbSource.value() .equals("KYV")){
DbSourceContext.setDbSource( SubjectUtil.getClientCode()+"_KYV");
return;
}else{
DbSourceContext.setDbSource(dbSource.value());
return;
}
}
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
dbSource = method.getAnnotation(DBSource.class);
if (dbSource != null) {
DbSourceContext.setDbSource(dbSource.value());
return;
}
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
dbSource = parameters[i].getAnnotation(DBSource.class);
if (dbSource != null) {
String dbSourceString = point.getArgs()[i].toString();
DbSourceContext.setDbSource(dbSourceString);
return;
}
}
SecurityUtils.setSecurityManager(securityManager);
String clientCode = SubjectUtil.getClientCode();
if (DbSourceContext.getDbSource() != null && clientCode == null) {
return;
}
DbSourceContext.setDbSource(clientCode);
} catch (Exception e) {
DbSourceContext.setDbSource(null);
}
}
@SuppressWarnings({})
@Before("execution(* com.pwc.digital.rc.service.survey.impl.SurveyManagementServiceImpl.autoSkipQuestionnaire(..)) || " +
"execution(* com.pwc.digital.rc.service.survey.impl.SurveyTypeManagementServiceImpl.getBySurveyTypeCode(..)) || " +
"execution(* com.pwc.digital.rc.service.survey.impl.SurveyManagementServiceImpl.buildUpMultiLangSurvey(..))")
public void switchDataSourceAutoSkip(JoinPoint point) {
try {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
DBSource dbSource = parameters[i].getAnnotation(DBSource.class);
if (dbSource != null) {
String dbSourceString = point.getArgs()[i].toString();
DbSourceContext.setDbSource(dbSourceString);
return;
}
}
SecurityUtils.setSecurityManager(securityManager);
String clientCode = SubjectUtil.getClientCode();
if (DbSourceContext.getDbSource() != null && clientCode == null) {
return;
}
DbSourceContext.setDbSource(clientCode);
} catch (Exception e) {
DbSourceContext.setDbSource(null);
}
}
}
我有这个电子邮件通知作业,应用程序开始连接到租户数据库,在那里它获得特定于客户端的数据库信息。在它从租户数据库切换并设置客户端数据库的上下文之后---
public void sendEmail(String clientCode) {
// --- questionnaire in process follow up
DbSourceContext.setDbSource(clientCode);
List<NotificationConfigEntity> allNotificationConfig = emailNotificationFacade.getAllNotificationConfig();
当对象试图 executesemailNotificationFacade.getAllNotificationConfig()
方法如何修复方面类或db源中的此错误..以便作业可以继续。。。用mybatis来做潜伏期。
暂无答案!
目前还没有任何答案,快来回答吧!