代码之家  ›  专栏  ›  技术社区  ›  Righto

Springboot多租户不适用于多线程

  •  0
  • Righto  · 技术社区  · 7 年前

    我引用了这个链接来实现两个数据源的springboot多租户-不同的数据库(尽管模式相同)- https://anakiou.blogspot.in/2015/08/multi-tenant-application-with-spring.html

    在我的应用程序中没有引入任何多线程之前,它工作得很好。

    当我添加了一个ExecutorService来为csv文件中的每个记录在多个表中进行插入时,我看到新线程不包含rest服务调用使用的原始租户标识符的信息。

    相反,它开始在新线程中使用默认租户。

    编辑1:ExecutorService代码:尝试将当前租户设置为:

    List<Future<Output>> futures = new ArrayList<Future<Output>>();
            for (int j = 0; j < myList.size(); j++) {
    
                final Output output= myList.get(j);
    
                Future<Output> future = executorService.submit(new Callable<Output>() {
                    @Override
                    public Output call() throws Exception {
                        **TenantContext.setCurrentTenant(<current tenant goes here>);**
                        Output currentOutput= someService.executeQueries(output);
                        return currentOutput;
                    }
                });
                futures.add(future);
            }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   aseychell    7 年前

    传播租户的正常方法是使用 ThreadLocal s、 在博客示例中,它使用类 RequestContextHolder 将整个请求存储在ThreadLocal中,然后从那里解析租户。