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

org.zodiac.datasource.jdbc.dynamic.DynamicDataSourceHolder Maven / Gradle / Ivy

The newest version!
package org.zodiac.datasource.jdbc.dynamic;

import java.util.Stack;

import org.zodiac.datasource.jdbc.util.DataSourceSpringUtil;
import org.zodiac.sdk.toolkit.thread.NamedThreadLocal;

public abstract class DynamicDataSourceHolder {

    /**
     * 使用栈存储的目的
     * 
     * 
     * 为了支持嵌套切换,如ABC三个service都是不同的数据源,其中A的某个业务要调B的方法,B的方法需要调用C的方法
     * 逐级调用切换,形成了链。传统的只设置当前线程的方式不能满足此业务需求,必须使用栈,后进先出。
     * 
*/ private static final ThreadLocal> HOLDER = new NamedThreadLocal>("dynamic-datasource") { @Override protected Stack initialValue() { return new Stack<>(); } }; public static void pushDataSource(String dataSource) { HOLDER.get().push(dataSource); } public static String peekDataSource() { if (!HOLDER.get().isEmpty()) { return HOLDER.get().peek(); } return DataSourceSpringUtil.getDynamicDefaultDataSource(); } public static String popDataSource() { if (!HOLDER.get().isEmpty()) { return HOLDER.get().pop(); } return DataSourceSpringUtil.getDynamicDefaultDataSource(); } public static void clearDataSource() { if (!HOLDER.get().isEmpty()) { HOLDER.get().pop(); if (HOLDER.get().isEmpty()) { HOLDER.remove(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy