我有一个每日计划的grails作业,它会根据上次日期的更改来更新用户的属性。它就像这样
try {
// Daily job for updating user orders
DateTime yesterday = new DateTime().withZone(DateTimeZone.getDefault()).withTimeAtStartOfDay().minusDays(1)
userService.updateStatsForAllUsers(yesterday)
}
catch (Exception e) {
println e.getCause()
}
UserService
void updateStatsForAllUsers(DateTime date) {
List<User> customers = User.list()
List<Order> orders = null
customers.each { customer ->
orders = customer.orders?.findAll{it.status.equals("DELIVERED")}?.sort{it?.dateCreated}
if (orders?.size() > 0) {
customer.firstOrderDate = orders?.first()?.dateCreated
customer.lastOrderDate = orders?.last()?.dateCreated
customer.totalOrders = orders.size()
customer.save(flush: true)
}
}
}
我们有一个大约50k的用户群,这个作业总是会在用户保存时抛出一个OptimisticLockingException。我们如何处理这个问题,有没有办法使用grails mongo进行这样的批量更新。