This commit is contained in:
罗祥 2019-01-27 08:51:30 +08:00
parent f6fbda5859
commit 90a70d50b6
8 changed files with 16 additions and 26 deletions

View File

@ -27,14 +27,14 @@ public class DynamicDataSourceAspect {
Object[] args = point.getArgs();
if (args == null || args.length < 1 || !Data.DATASOURCE2.equals(args[0])) {
DataSourceContextHolder.setDataKey(Data.DATASOURCE1);
DataSourceContextHolder.setDataSourceKey(Data.DATASOURCE1);
} else {
DataSourceContextHolder.setDataKey(Data.DATASOURCE2);
DataSourceContextHolder.setDataSourceKey(Data.DATASOURCE2);
}
}
@After(value = "dataSourcePointCut()")
public void afterSwitchDS(JoinPoint point) {
DataSourceContextHolder.clearDataKey();
DataSourceContextHolder.clearDataSourceKey();
}
}

View File

@ -79,7 +79,7 @@ public class CustomSqlSessionTemplate extends SqlSessionTemplate {
@Override
public SqlSessionFactory getSqlSessionFactory() {
String dataSourceKey = DataSourceContextHolder.getDataKey();
String dataSourceKey = DataSourceContextHolder.getDataSourceKey();
log.info("当前会话工厂 : {}", dataSourceKey);
SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactories.get(dataSourceKey);
if (targetSqlSessionFactory != null) {

View File

@ -9,17 +9,17 @@ public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
// 设置数据源名
public static void setDataKey(String dbName) {
public static void setDataSourceKey(String dbName) {
contextHolder.set(dbName);
}
// 获取数据源名
public static String getDataKey() {
public static String getDataSourceKey() {
return (contextHolder.get());
}
// 清除数据源名
public static void clearDataKey() {
public static void clearDataSourceKey() {
contextHolder.remove();
}
}

View File

@ -10,6 +10,7 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@ -50,7 +51,7 @@ public class DataSourceFactory {
}
/**
* 创建支持XA事务的数据源1不要用@Primary去修饰
* 创建支持XA事务的Atomikos数据源1不要用@Primary去修饰
*/
@Bean
public DataSource dataSourceOne(DataSource DruidDataSourceOne) {
@ -61,7 +62,7 @@ public class DataSourceFactory {
}
/**
* 创建支持XA事务的数据源2不要用@Primary去修饰
* 创建支持XA事务的Atomikos数据源2不要用@Primary去修饰
*/
@Bean
public DataSource dataSourceTwo(DataSource DruidDataSourceTwo) {

View File

@ -11,7 +11,7 @@ import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
log.info("当前数据库:{}",DataSourceContextHolder.getDataKey());
return DataSourceContextHolder.getDataKey();
log.info("当前数据库:{}",DataSourceContextHolder.getDataSourceKey());
return DataSourceContextHolder.getDataSourceKey();
}
}

View File

@ -62,6 +62,7 @@ spring:
removeAbandoned: true
# 超时时间(以秒数为单位)
remove-abandoned-timeout: 1800
# WebStatFilter用于采集web-jdbc关联监控的数据。
web-stat-filter:
# 是否开启 WebStatFilter 默认是true

View File

@ -1,15 +0,0 @@
{"id":"10.0.75.1.tm154841018873800001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"COMMITTING","expires":1548410199513,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"COMMITTING","expires":1548410199513,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841018873800001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"TERMINATED","expires":1548410199530,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"TERMINATED","expires":1548410199530,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841019225200002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"COMMITTING","expires":1548410202212,"resourceName":"db1"},{"uri":"10.0.75.1.tm4","state":"COMMITTING","expires":1548410202212,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841019225200002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"TERMINATED","expires":1548410202229,"resourceName":"db1"},{"uri":"10.0.75.1.tm4","state":"TERMINATED","expires":1548410202229,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841019386700003","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm5","state":"COMMITTING","expires":1548410203812,"resourceName":"db1"},{"uri":"10.0.75.1.tm6","state":"COMMITTING","expires":1548410203812,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841019386700003","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm5","state":"TERMINATED","expires":1548410203832,"resourceName":"db1"},{"uri":"10.0.75.1.tm6","state":"TERMINATED","expires":1548410203832,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841022094600004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"COMMITTING","expires":1548410230871,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841022094600004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"TERMINATED","expires":1548410230877,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841022770300005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm8","state":"COMMITTING","expires":1548410237617,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841022770300005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm8","state":"TERMINATED","expires":1548410237623,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841023952400006","wasCommitted":false,"participants":[{"uri":"10.0.75.1.tm9","state":"TERMINATED","expires":1548410249451,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841026359700007","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm10","state":"COMMITTING","expires":1548410273511,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841026359700007","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm10","state":"TERMINATED","expires":1548410273518,"resourceName":"db1"}]}
{"id":"10.0.75.1.tm154841026807100008","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm11","state":"COMMITTING","expires":1548410278015,"resourceName":"db1"},{"uri":"10.0.75.1.tm12","state":"COMMITTING","expires":1548410278015,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154841026807100008","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm11","state":"TERMINATED","expires":1548410278033,"resourceName":"db1"},{"uri":"10.0.75.1.tm12","state":"TERMINATED","expires":1548410278033,"resourceName":"db2"}]}

View File

@ -0,0 +1,3 @@
{"id":"10.0.75.1.tm154848923394100001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"COMMITTING","expires":1548489244031,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"COMMITTING","expires":1548489244031,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154848923394100001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"TERMINATED","expires":1548489244061,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"TERMINATED","expires":1548489244061,"resourceName":"db2"}]}
{"id":"10.0.75.1.tm154848925504500002","wasCommitted":false,"participants":[{"uri":"10.0.75.1.tm3","state":"TERMINATED","expires":1548489265019,"resourceName":"db1"},{"uri":"10.0.75.1.tm4","state":"TERMINATED","expires":1548489265019,"resourceName":"db2"}]}