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

xyz.shodown.dynamicdb.context.DynamicContextHolder Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.xhyj.io
 *
 * 版权所有,侵权必究!
 */

package xyz.shodown.dynamicdb.context;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 多数据源上下文
 *
 * @author [email protected]
 */
public class DynamicContextHolder {

    private static final ThreadLocal> CONTEXT_HOLDER = ThreadLocal.withInitial(ArrayDeque::new);

    /**
     * 获得当前线程数据源
     *
     * @return 数据源名称
     */
    public static String peek() {
        return CONTEXT_HOLDER.get().peek();
    }

    /**
     * 设置当前线程数据源
     *
     * @param dataSource 数据源名称
     */
    public static void push(String dataSource) {
        CONTEXT_HOLDER.get().push(dataSource);
    }

    /**
     * 清空当前线程数据源
     */
    public static void poll() {
        Deque deque = CONTEXT_HOLDER.get();
        deque.poll();
        if (deque.isEmpty()) {
            CONTEXT_HOLDER.remove();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy