gpt4 book ai didi

multithreading - Grails 1.3.7- Multi-Tenancy 插件和多线程操作-线程无法访问租户

转载 作者:行者123 更新时间:2023-12-02 14:40:21 26 4
gpt4 key购买 nike

如标题所示,我的情况是:

  • 我使用的是Grails的非常旧的版本(目前无法更改)
  • 在此项目中,我正在使用 Multi-Tenancy 插件,详细而言,我已经安装了该插件
  • Multi-Tenancy 核心:1.0.3
  • Multi-Tenancy ehcache:1.0.1
  • 我想拆分一个方法(执行它需要2-3分钟)并并行化操作。

  • 这是我的代码:
    def threadPoolSize = 10 
    def threadPool = Executors.newFixedThreadPool(threadPoolSize)

    def threadClosure = { myWork ->
    def partialQuantity = 0
    myWork.each { currDetail ->
    MyTableDomainClass.findByCode(currDetail.myTableCode)
    // Do some stuff
    }
    return partialQuantity
    }

    try{
    def worksForThreads = new ArrayList<org.codehaus.groovy.grails.web.json.JSONArray>(10)

    // Prepare works for thread
    Integer x = 0
    allWorks.each{ singleOrder ->
    if(worksForThreads[x] == null)
    worksForThreads[x] = new org.codehaus.groovy.grails.web.json.JSONArray()
    worksForThreads[x].add(singleOrder)
    x = (x+1) % threadPoolSize
    }

    List<Future> futures = worksForThreads.collect({ myWork ->
    println "\t\tPrepare thread with ${myWork.size()} tasks"
    threadPool.submit({ ->
    threadClosure myWork
    } as Callable )
    })

    //Start thread and collect result
    futures.each{
    def threadResult = it.get()
    println "\t\tThread result ${threadResult}"
    totQuantity = totQuantity+threadResult
    }
    }catch(Exception e){
    println "Error during thread works ${e}"
    }finally{
    threadPool.shutdown()
    }

    因此,代码应该没问题,但是在线程执行期间,出现以下错误:
    [pool-9-thread-2] [tenant 0] ERROR util.JDBCExceptionReporter  - Table 'MySchema.MyTable' doesn't exist


    Error during thread works java.util.concurrent.ExecutionException: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; 
    SQL [select this_.id as id24_0_, this_.code as code24_0_, this_.description as descript3_24_0_, this_.incr as incr24_0_, this_.is_default as is5_24_0_, this_.lang as lang24_0_, this_.last_updated as last7_24_0_, this_.min as min24_0_, this_.name as name24_0_, this_.ordasc as ordasc24_0_, this_.parent_id as parent11_24_0_, this_.parent_mult as parent12_24_0_, this_.prod_code as prod13_24_0_, this_.status as status24_0_, this_.unit_name as unit15_24_0_
    from MyTable this_ where this_.code=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

    我认为问题在于租户范围。我曾想过使用一种服务来从数据库中获取所有数据,但是我想知道是否有一种方法可以在线程中获取正确的租户范围或将其传递。

    谢谢大家!

    最佳答案

    我找到了解决方案。基本上, Multi-Tenancy 插件通过数据库表获取租户:

    所以在Config.groovy中,我有

    tenant {
    mode = "singleTenant"
    datasourceResolver.type = "db"
    }

    并且DataSource.groovy为每个环境提供正确的表,以查找租户:
    CREATE TABLE `data_source_tenant_map` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `version` bigint(20) NOT NULL,
    `data_source` varchar(255) NOT NULL COMMENT 'JNDI',
    `mapped_tenant_id` int(11) NOT NULL,
    `file_source` varchar(255) NOT NULL,
    `data_source_secondary` varchar(255) DEFAULT NULL,
    `db_url` varchar(1000) DEFAULT NULL COMMENT 'Url db multitenant',
    `db_username` varchar(100) DEFAULT NULL COMMENT 'Username db multitenant',
    `db_password` varchar(100) DEFAULT NULL COMMENT 'Password db multitenant',
    `base_path` varchar(255) DEFAULT NULL COMMENT 'Root of path on fs',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8;

    使用此插件,每个未与主线程连接的线程都会丢失Hibernate session ,因此租户引用。为了获得合适的租户,我使用了以下方法:
    def myTenantKey = my_mapped_tenant_id
    TenantUtils.doWithTenant(myTenantKey ) {
    // Do something as usual
    }

    您必须导入TenantUtils类
    import grails.plugin.multitenant.core.util.TenantUtils

    希望对其他人有用!
    再见!

    关于multithreading - Grails 1.3.7- Multi-Tenancy 插件和多线程操作-线程无法访问租户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288704/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com