English 中文(简体)
杯子:什么确保版本检查和更新是原子?
原标题:Grails: What makes sure that version check and update is atomic?

The update action in Grails first checks for the version of the object to be updated and then updates it. What part of Grails ensures that the object isn t updated by another request during checking the version and updating the object?

<强> 更新:

是的, 冬眠将在保存对象时检查版本, 并会丢出一个例外是乐观锁定失败。 我想冬眠会确保检查+更新是原子, 但是...

如果您查看一下生成的更新方法, 您就会发现, 第一次重复检查后( 从我的角度), (然后) ( 从我的观点) 将无法处理例外 。 在更新方法已经检查过正确版本后, 冬眠会丢出例外的可能性很小, 但对我来说是可能的 。

试一试省钱,抓住例外(如果有例外),难道还不够吗?

最佳回答

它由休眠层管理。 它叫做乐观锁定, 基本上它只用已知版本更新对象。 例如 :

UPDATE SET
  %... fields ...%, 
  version = version + 1                      --update version to a new value
WHERE 
  id = %obj id%                              --current object
  AND version = %previous obj version%`      --last known version 

当它未更新时, 丢弃例外( btw, 目前很难从错误中恢复, 在大多数情况下您只是丢失更新) 。

如果您想要确定数据被保存, 请尝试强制数据保存( 检查保存/ 校验错误) :

try {
    if (!obj.save(flush: true)) {
        // validation error
    }
} catch (OptimisticLockingFailureException e) {
    // not saved
}

或甚至在更新前锁定数据。 只有当您同时有大量更新时, 它才有用 。

MyDomain obj = MyDomain.lock(params.id) //now it s locked for update
// update fields
obj.save()

有关GORM锁定的更多详情,见http://grails.org/doc/latest/guide/GORM.html#locking>rel=“nofollow”>http://grails.org/doc/latst/guide/GORM.html#locking

问题回答

暂无回答




相关问题
grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

Error loading the grails gwt module xml

I ve installed the plugin from this article by Peter http://www.cacoethes.co.uk/blog/groovyandgrails/the-command-pattern-w.... While compile time its not able to find the module file which is present ...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

热门标签