Grails 2.5.1 domain.get(id)获取缓存数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grails 2.5.1 domain.get(id)获取缓存数据相关的知识,希望对你有一定的参考价值。
我是groovy和grails的新手。 Grails版本2.5.1 Groovy版本2.4.5我的问题是:我在customerController中有一个更新方法。
customerController:
def update(){
def map = [:]
def customerId = params.id
def newCustomer = request.JSON
def customer = customerService.updateCustomer(customerId, newCustomer)
customer.validate()
if (customer== null) {
map['success'] = false
map['message'] = "Record not found"
return map
}
if (customer.save()) {
map['success'] = true
map['data'] = customer
} else {
validateErrorService.extractErrors(customer, map)
}
return map
}
在更新方法中,我致电客户Service.update客户
客户服务:
def updateCustomer(customerId, newCustomer){
def customer = Customer.get(customerId)
if (customer == null) {
return null
}
verifyLead(newCustomer.leadId)
customer.firstName = newCustomer.firstName
customer.lastName = newCustomer.lastName
if(!customer.hasErrors())
customer.save(flush:true)
return customer
}
然后我调用verifyLead方法来更新Lead域
def verifyLead(jsonLead, userId){
Long leadId = jsonLead.id as Long
Lead lead = Lead.get(leadId)
lead.status = "Proceed"
lead.save(flush: true)
}
在lead domain方法中,Lead.get(leadId)返回旧的(缓存的)数据所以当我保存:lead.save(flush:true)时出现错误hibernate.object.staleException
(我在这里推测,因为我不知道你的设置)
简短回答:通常会发生StaleObjectStateException(我想你得到的)当域对象(在你的情况下是Lead)后面的数据库记录在最后一次从DB读取和你的save()
调用之间更新时发生。根据您的设置,它可能是Grails应用程序的第二个实例。
一种可能的解决方案可能是悲观锁定(请参阅Grails文档中的7.3.5 Pessimistic and Optimistic Locking)。
更长的答案(对不起,如果你已经知道更多的技术背景):get()
从底层的Hibernate会话中返回一个域对象。此会话无法自动识别数据库中的更改。因此,如果数据库发生更改,那么Hibernate会话中的数据可能会变得陈旧。 GORM(Grails的DB层)通常使用乐观锁定(尝试保存并增加DB记录的version
字段的值)。
如果您的代码尝试保存的域对象的版本小于db中对象的版本,则GORM / Hibernate认为抛出异常比更新记录更好,并且可能会破坏更新的更改。
使用悲观锁定,您的代码首先锁定DB记录或表,然后更新记录,最后释放锁。
但要小心:悲观锁定带来了它自己的问题。
以上是关于Grails 2.5.1 domain.get(id)获取缓存数据的主要内容,如果未能解决你的问题,请参考以下文章