0

你可以通過標題我的情況是瞭解:Grails的1.3.7 - 多租戶的插件和多線程運行 - 線程不能訪問租戶

  • 我用一個很老的版本的Grails的(我此刻)
  • 在這個項目中我使用多租戶的插件不能改變它,在細節我已經安裝了
    • 多租戶核心:1.0.3
    • 多租戶-的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 

我認爲問題出在租戶範圍之內。我想用一種服務來獲取數據庫中的所有數據,但我想知道是否有辦法在線程中獲得正確的租戶作用域或傳遞它。

謝謝大家!

回答

0

我找到了一個解決方案。所以

在Config.groovy中我有

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

而且DataSource.groovy中提供每個環境的正確表,其中找出tennants:基本上多租戶的插件由數據庫表中提取tennants

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會話,所以租戶參考。爲了得到我用這些方法的權利租戶:

def myTenantKey = my_mapped_tenant_id 
TenantUtils.doWithTenant(myTenantKey) { 
    // Do something as usual 
} 

您必須導入TenantUtils類

import grails.plugin.multitenant.core.util.TenantUtils 

我希望它可以爲別人有用! 再見!