[英]GORM Object Relation is not loaded in an async block
I use Grails GPaars to create an async block. 我使用Grails GPaars创建一个异步块。
In BuildConfig: 在BuildConfig中:
compile 'org.codehaus.gpars:gpars:1.2.1'
compile 'org.codehaus.jsr166-mirror:jsr166y:1.7.0'
I defined a helper Class: 我定义了一个助手类:
class TaskService {
private ForkJoinPool pool = new ForkJoinPool()
/**
* Executes the given closure in a new thread.
* @param args is a map of arguments to be used in the async closure.
* @return
*/
def executeAsync(args, closure = null) {
if(!closure) {
closure = args
args = null
}
GParsPool.withExistingPool(pool) { closure.callAsync(args) }
}
}
Now in a controller I do: 现在在控制器中执行以下操作:
TrackingEmail tEmail = TrackingEmail.get(trackingEmailId)
Device targetDevice = tEmail.device
The former works the device
is retrieved from the TrackingEmail object. 从TrackingEmail对象检索device
的前者作品。
Now I try to do the same in an async block: 现在,我尝试在异步块中执行相同的操作:
taskService.executeAsync(trackingEmailId: trackingEmailId) { data ->
TrackingEmail tEmail = TrackingEmail.get(data.trackingEmailId)
Device targetDevice = tEmail.device
}
In this async block only tEmail
is retrieved from the database. 在此异步块中,仅tEmail
从数据库中检索。 The second line is not executed. 第二行不执行。
How do I get relational objects in an async block? 如何在异步块中获取关系对象?
I don't think you should use GPars for GORMing directly. 我认为您不应该直接将GPar用于GORMing。 In this case you would have to take care of transactions/sessions yourself: 在这种情况下,您将不得不自己处理事务/会话:
taskService.executeAsync(trackingEmailId: trackingEmailId) { data ->
Device.withTransaction{ tx ->
TrackingEmail tEmail = TrackingEmail.get(data.trackingEmailId)
Device targetDevice = tEmail.device
}
}
and that would give you bad performace in case of frequent parallel calls. 在频繁进行并行调用的情况下,这将使您的性能下降。
I'd recommend to take a look at Asynchronous Programming in Grails , which uses the extended GPars 我建议看一下Grails中的异步编程 ,它使用扩展的GPar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.