Home Grails 2.3.6 not throwing optimistic exceptions
Reply: 1

Grails 2.3.6 not throwing optimistic exceptions

Jim Leggit
Jim Leggit Published in 2014-03-03 03:12:56Z

I have a scaffolded domain class and I am trying to get it to throw an optimistic locking exception, but it won't. I put the same instance in edit mode in two different browsers. I edit a field in browser 1 and click update. I verified that the version increments. I then edit in browser 2 and click update. It just overwrites the browser 1 changes with the browser 2 changes. I've seen many questions here about people getting the exception when they don't want it, so I am wondering if they turned it off in Hibernate somehow. I even tried changing the update method so all it does is execute instance.save(). Am I not understanding what optimistic locking is supposed to do?

package demo
class issue {
    String synopsis
    String details

    static constraints = {
        details(size: 0..1000)
injecteer Reply to 2014-03-04 09:02:46Z

I haven't tried Grails 2.3.x, but in versions up to 2.2.4 the version check is happening exactly in update action and not deep inside hibernate:

def update(Long id, Long version) {
    def priceInstance = Price.get(id)
    if (version != null) {
        if (priceInstance.version > version) {
            priceInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'price.label', default: 'Price')] as Object[],
                      "Another user has updated this Price while you were editing")
            render(view: "edit", model: [priceInstance: priceInstance])

    priceInstance.properties = params

    if (!priceInstance.save(flush: true)) {
        render(view: "edit", model: [priceInstance: priceInstance])

if you are not using that or similar code, GORM stores the instance as it is without checking for version

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346855 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO