cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.dsource;
 
import org.springframework.util.Assert;
 
public class DataSourceSwitcher {
    @SuppressWarnings("rawtypes")  
    private static final ThreadLocal contextHolder = new ThreadLocal();  
  
    @SuppressWarnings("unchecked")
    public static void setDataSource(String dataSource) {  
        Assert.notNull(dataSource, "dataSource cannot be null");  
        contextHolder.set(dataSource);  
    }  
  
    public static void setMaster(){  
        clearDataSource();  
    }  
      
    public static void setSlave(int num) {  
        setDataSource("slave"+num);  
    }  
      
    public static String getDataSource() {  
        return (String) contextHolder.get();  
    }  
  
    public static void clearDataSource() {  
        contextHolder.remove();  
    }  
}