DynamicDataSourceContextHolder.java 850 Bytes
  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
package com.lyms.etl.datasource;

import java.util.ArrayList;
import java.util.List;

/**
* 通过ThreadLocal来存储当前所使用数据源对应的key
* @Author: litao
* @Date: 2017/5/17 0017 10:25
* @Version: V1.0
*/
public class DynamicDataSourceContextHolder {

private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

public static List<String> dataSourceIds = new ArrayList<>();

public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}

public static String getDataSourceType() {
return contextHolder.get();
}

public static void clearDataSourceType() {
contextHolder.remove();
}

public static boolean containsDataSource(String dataSourceId){
return dataSourceIds.contains(dataSourceId);
}
}