0
votes

In Grails, is there a way to ignore optimistic locking for 1 save and use it in another? I have an integration service that updates some meta data to a domain instance once a while, and I don't want that it locks a form. Can I e.g. change the object's version property on fly or ignore raising the version number during save process? Or is there a way to ignore an OptimisticLockingFailureException during save process? I would like to disable optimistic locking check in that 1 part of code only!

I have tried following with no luck - It always throws "The instance was updated by another user while you were editing":

            // Validate before optimistic locking hacking - Save is done with validate: false
            if (filledForm && filledForm.validate() == true) {

                // Do not update version number (optimistic locking) on save
                if(filledForm.version > 0){

                    filledForm.version = filledForm.version - 1
                }

                filledForm.save(validate:false, deepValidate: false, flush:true, failOnError:false)

            }

I'm using Grails 2.3.8 with MongoDB-plugin 3.0.0

4
SQL/HQL may work when using a relational database, but in this case, I'm using MongoDB. The MongoDB document I'm trying to save holds multiple levels of embedded documents. Using pure Gmongo could be the answer, but I haven't been able to convert GORM instance to a map successfully. However, I'm also interested how it's done with MySQL database, but the best solution would affect them both. Is there any closures, configurations or save attributes that could be used to achieve the right solution? - niko.makela

4 Answers

1
votes

Use MongoDB low-level API for updating the document to ignore the GORM level checks.

http://api.mongodb.org/java/current/com/mongodb/DBCollection.html

FormDomain.collection.findAndModify()
0
votes

as a dirty hack you can run a HQL query to decrement your version after the "propper" save()

0
votes

By default GORM is configured with optimistic locking enabled. You can disable this by calling the version method with an argument of false:

In your domain class, set a mapping like this:

static mapping = {
    // Used to disable optimistic locking
    version false
}
-1
votes

If it's something simple, I'd say just inject your dataSource and use Groovy SQL to update the property directly:

def dataSource
...

def sql = new Sql(dataSource)
sql.executeUpdate('update something set foo = ? where id = ?', ['bar', 1234L])

Hibernate has more domain-level options for only version checking dirty properties (optimistic-lock="dirty") but I'm not sure that Grails supports them.