Bootstrap

MybatisPlus多数据源使用多线程时失效的问题

 

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

    @Override
    public Future<Map<String, Object>> getTableOverview(CountDownLatch latch) {
        return executor.submit(new Callable<Map<String, Object>>() {
            @DS("slave")
            @Override
            public Map<String, Object> call() throws Exception {
                // 设置以后线程数据源
                DynamicDataSourceContextHolder.push("slave");
                Map<String, Object> result;
                try {
                    // 查询数据库办法
                    result = getTableData();
                } finally {
                    latch.countDown();
                    // 强制清空本地线程,避免内存透露,手动调用push可调用此办法确保革除
                    DynamicDataSourceContextHolder.clear();
                }
                return result;
            }
        });
    }
;