package org.jeecg.common.handler;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;

import java.util.function.Supplier;

public class MultiDatasourceHandler {

    public static final String MASTER = "master";
    public static final String SYSTEM = "system";

    private static final String[] DS = {MASTER, SYSTEM};

    public static <T> T execute(Supplier<T> callback) {
        for (String ds : DS) {
            DynamicDataSourceContextHolder.push(SYSTEM);
            try {
                T result = callback.get();
                if (result != null) {
                    return result;
                }
            } finally {
                DynamicDataSourceContextHolder.clear();
            }
        }
        return null;
    }


}
