All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.mybatis.mp.routing.datasource.DataSourceHolder Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy