-
Notifications
You must be signed in to change notification settings - Fork 86
/
MyAbstractRoutingDataSource.java
39 lines (34 loc) · 1.15 KB
/
MyAbstractRoutingDataSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ggj.encrypt.common.utils.mybatis;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.util.StringUtils;
/**
* 多数据源切换
* @author:gaoguangjin
* @date 2016/5/30 14:34
*/
@Slf4j
public class MyAbstractRoutingDataSource extends AbstractRoutingDataSource {
private final int dataSourceNumber;
private AtomicInteger count = new AtomicInteger(0);
public MyAbstractRoutingDataSource(int dataSourceNumber) {
this.dataSourceNumber = dataSourceNumber;
}
@Override
protected Object determineCurrentLookupKey() {
Object resultObject=null;
String typeKey = DataSourceContextHolder.getJdbcType();
//只对主库开启事务,如果typeKey为空表示获取主库的datasource
if (StringUtils.isEmpty(typeKey)){
resultObject= DataSourceType.write.getType();
}else{
// 读简单负载均衡
int number = count.getAndAdd(1);
int lookupKey = number % dataSourceNumber;
resultObject= new Integer(lookupKey);
}
log.info("determineCurrentLookupKey:"+resultObject);
return resultObject;
}
}