列表中的一个域对象上的StaleObjectStateException会影响所有剩余的更新 [英] StaleObjectStateException on one domain object in a list affects all remaining updates
问题描述
我在Grails中有一个Quartz作业,它可以迭代我的用户并对每个用户进行每晚更新。但是,如果我在用户对象的更新上遇到StaleObjectStateException,那么看起来每个更新都会得到相同的StaleObjectStateException。像这样:
def users = User.list()
users.each {user - >
try {
user.doUpdate()
user.save()
} catch(all){
// //我在这里结束每个用户对象后StaleObjectStateException
$ / code $ / pre
我该如何恢复?我不介意偶尔发生的故障(理想情况下,我收集这些信息并在最后再次尝试),但是现在这会逐步停止所有剩余的更新。
理想情况下,你应该每做一次更新/保存一个新事务,否则任何失败都会影响整个事务/休眠会话。这更好理解,因为每次更新/保存都应该是它自己的原子操作。
您还应该在事务内部获取每个域对象。
def userIds = User.withCriteria {
预测{
属性(id)
}
}
userIds.each {userId - >
User.withTransaction {
try {
def user = User.get(userId)
user.doUpdate()
user.save()
}赶上(全部){
//做任何事情。
}
}
}
I have a Quartz job in Grails that iterates my users and do a nightly update on each user. However, if I get a StaleObjectStateException on an update of a user object, it seems every update after that gets the same StaleObjectStateException. Something like this:
def users = User.list()
users.each { user ->
try {
user.doUpdate()
user.save()
} catch (all) {
// I end up here for every user object after a StaleObjectStateException
}
}
How can I recover? I don't mind sporadic failures (ideally I collect these and try again at the end), but now this literally stops all remaining updates.
Ideally you should be doing each update/save in a new transaction, otherwise any failures will effect the entire transaction / hibernate session. This makes better sense since each update/save should be it's own atomic operation anyway.
You should also get each domain object inside the transaction.
def userIds = User.withCriteria {
projections {
property("id")
}
}
userIds.each { userId ->
User.withTransaction {
try {
def user = User.get(userId)
user.doUpdate()
user.save()
} catch (all) {
// do whatever.
}
}
}
这篇关于列表中的一个域对象上的StaleObjectStateException会影响所有剩余的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!