【问题标题】:Springboot Multi-tenancy does not work with multi-threadingSpringboot 多租户不适用于多线程
【发布时间】:2017-10-05 01:44:24
【问题描述】:

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

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

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

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

我们如何解决这个问题?将非常感谢任何指针。

编辑 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);
        }

【问题讨论】:

    标签: multithreading hibernate spring-boot spring-data-jpa multi-tenant


    【解决方案1】:

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

    当您更改线程时,线程局部变量会在新线程中丢失,除非您重新设置它们。

    【讨论】:

    • 谢谢!在您回答后,我在描述中提供了 EDIT 1,以检查在创建多个线程时如何设置当前租户。这是你的意思吗?
    猜你喜欢
    • 2020-03-26
    • 2018-04-07
    • 2019-09-20
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多