我有一个全球编号,该编号经常增加,但每年重置一次。现在,我将此全局编号存储为单行:
class GlobalCounter {
Integer counter = 0
static constraints = {
}
}
当我增加它时,我在事务中使用了一个锁:
def globalCounter = GlobalCounter.lock(1)
globalCounter.counter = globalCounter.counter + 1
globalCounter.save()
globalCounter.discard()
问题是,当我频繁更新它时,我得到一个异常而不是等待代码:
Transaction (Process ID 61) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
我知道,当您添加新行时,数据库提供了使用具有自动增量功能的主键的选项,因此我认为这应该是没有死锁的。我该怎么办?