我一直在关注 Grails 提供的关于 Promise 的文档: https ://docs.grails.org/2.3.x/guide/async.html#promises
我有一个巨大的 id 列表,我将它们分成不同的列表tasks
,然后调用waitAll()
promise 列表。基于此,当其中一个并发任务抛出异常时,是否可以回滚所有更改?
查看代码可能更容易:
def promistList = []
def collatedList = hugeList.collate(100)
collatedList.each {
promiseList << task {
DomainObject.withTransaction {
def domainObject = findById('test')
// do more work
domainObject.save()
}
}
}
waitAll(promiseList)
当一切正常时,这很有效,但当然,当事情进展不顺利时,我必须做好准备。理想情况下,即使最后一个任务抛出异常,我也想回滚所有任务中所做的所有更改。
这可能吗?