package me.zhengjie;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    @Override
    protected Object determineCurrentLookupKey() {
        System.out.println("get:" + contextHolder.get());
        return contextHolder.get();
    }

    public static void setCurrentLookupKey(String key) {
        System.out.println("set:" + key);
        contextHolder.set(key);
    }

    public static void clearCurrentLookupKey() {
        contextHolder.remove();
    }
}