cn.mybatis.mp.routing.datasource.DataSourceHolder Maven / Gradle / Ivy
package cn.mybatis.mp.routing.datasource;
import java.util.ArrayDeque;
import java.util.Deque;
public class DataSourceHolder {
private static final ThreadLocal> dataSource = new ThreadLocal<>();
public static void add(String type) {
Deque list = dataSource.get();
if (list == null) {
list = new ArrayDeque<>();
}
list.addFirst(type);
dataSource.set(list);
}
public static String getCurrent() {
Deque list = dataSource.get();
if (list == null || list.isEmpty()) {
return null;
}
return list.getFirst();
}
public static void remove() {
Deque list = dataSource.get();
if (list == null || list.isEmpty()) {
dataSource.remove();
return;
}
int size = list.size();
if (size == 1) {
list.clear();
dataSource.remove();
return;
}
list.removeFirst();
}
}