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

com.github.xuyuansheng.xbdynamicdatasource.dynamic.DynamicDataSourceContextHolder Maven / Gradle / Ivy

package com.github.xuyuansheng.xbdynamicdatasource.dynamic;

/**
 * @author xuyuansheng
 * @date 2019-12-10 14:53
 */
public class DynamicDataSourceContextHolder {

    private static final ThreadLocal CONTEXTHOLDER = new ThreadLocal<>();

    public static synchronized void setDataSourceKey(String key) {
        checkState(null == CONTEXTHOLDER.get(), "ContextHolder 已经设置过值了,请先清除!.");
        CONTEXTHOLDER.set(key);
    }

    /**
     * Get current DataSource
     *
     * @return data source key
     */
    public static String getDataSourceKey() {
        return CONTEXTHOLDER.get();
    }

    /**
     * To set DataSource as default
     */
    public static void clearDataSourceKey() {
        CONTEXTHOLDER.remove();
    }

    public static void checkState(boolean expression, Object errorMessage) {
        if (!expression) {
            throw new IllegalStateException(String.valueOf(errorMessage));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy