Java GORM对象关系未加载到异步块中
我使用grailsgpaars创建一个异步块 在BuildConfig中:Java GORM对象关系未加载到异步块中,java,grails,asynchronous,groovy,gorm,Java,Grails,Asynchronous,Groovy,Gorm,我使用grailsgpaars创建一个异步块 在BuildConfig中: compile 'org.codehaus.gpars:gpars:1.2.1' compile 'org.codehaus.jsr166-mirror:jsr166y:1.7.0' 我定义了一个助手类: class TaskService { private ForkJoinPool pool = new ForkJoinPool() /** * Executes the given closure
compile 'org.codehaus.gpars:gpars:1.2.1'
compile 'org.codehaus.jsr166-mirror:jsr166y:1.7.0'
我定义了一个助手类:
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) }
}
}
现在在控制器中,我执行以下操作:
TrackingEmail tEmail = TrackingEmail.get(trackingEmailId)
Device targetDevice = tEmail.device
前者在从TrackingEmail对象检索设备时起作用
现在,我尝试在异步块中执行相同的操作:
taskService.executeAsync(trackingEmailId: trackingEmailId) { data ->
TrackingEmail tEmail = TrackingEmail.get(data.trackingEmailId)
Device targetDevice = tEmail.device
}
在此异步块中,仅从数据库检索tEmail
。第二行未执行
如何在异步块中获取关系对象?我认为不应该直接使用GPAR进行GORMing。在这种情况下,您必须自己处理事务/会话:
taskService.executeAsync(trackingEmailId: trackingEmailId) { data ->
Device.withTransaction{ tx ->
TrackingEmail tEmail = TrackingEmail.get(data.trackingEmailId)
Device targetDevice = tEmail.device
}
}
在频繁的并行调用情况下,这会给您带来糟糕的性能
我建议看一看,它使用扩展GPAR是的!我已经成功地使用了grails.async.promissions.task。它确实需要.withTransaction()。