Commit 8157fa11 authored by eddie.woo's avatar eddie.woo

modify

parent a42ec78c
package pwc.taxtech.atms.common.datasource;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
@Aspect
public class AopShardingHolder {
private static Logger logger = LoggerFactory.getLogger(AopShardingHolder.class);
private static final String DATABASE_NAME_HEADER = "from";
private static final String DATABASE_NAME_SPLIT = "@";
@Autowired
HttpServletRequest request;
@Pointcut("execution(public * pwc.taxtech.atms.controller.*.*(..))")
public void pointCut() {
// 定义Pointcut 此方法不能有返回值,该方法只是一个标示
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
try {
String header = request.getHeader(DATABASE_NAME_HEADER);
if (StringUtils.isNotBlank(header)) {
if (logger.isDebugEnabled()) {
logger.debug("aop set datasource:{}", header);
}
String[] arr = StringUtils.split(header, DATABASE_NAME_SPLIT);
String name = arr[0];
if (name.length() > 0 && !StringUtils.contains(name, DATABASE_NAME_SPLIT)) {
ShardingContextHolder.setDataSourceKey(name);
}
}
} catch (Exception e) {
logger.error("aop set database error.", e);
}
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
}
@AfterThrowing(pointcut = "pointCut()", throwing = "error")
public void afterThrowing(JoinPoint joinPoint, Throwable error) {
}
}
\ No newline at end of file
...@@ -42,6 +42,8 @@ public class DataSourcePoolService extends AbstractRoutingDataSource { ...@@ -42,6 +42,8 @@ public class DataSourcePoolService extends AbstractRoutingDataSource {
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig(); GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setTestOnBorrow(true); poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true); poolConfig.setTestOnReturn(true);
poolConfig.setMaxTotalPerKey(1);
poolConfig.setMaxIdlePerKey(1);
dataSourcePool = new GenericKeyedObjectPool<>(new DataSourceFactory(config), poolConfig); dataSourcePool = new GenericKeyedObjectPool<>(new DataSourceFactory(config), poolConfig);
this.setTargetDataSources(Maps.newHashMap()); this.setTargetDataSources(Maps.newHashMap());
} }
...@@ -52,7 +54,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource { ...@@ -52,7 +54,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource {
} }
@Override @Override
protected DataSource determineTargetDataSource() { protected synchronized DataSource determineTargetDataSource() {
try { try {
String key = ShardingContextHolder.getDataSourceKey(); String key = ShardingContextHolder.getDataSourceKey();
DruidDataSource dataSource = dataSourcePool.borrowObject(key); DruidDataSource dataSource = dataSourcePool.borrowObject(key);
......
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
<listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param> <context-param>
<param-name>contextConfigLocation</param-name> <param-name>contextConfigLocation</param-name>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment