Search code examples
grailsgrails-ormpessimistic-locking

pessimistic locking in GORM where query


I'd like to achieve pessimistic lock with GORM's where query.

Something like

def query = Person.where {
    firstName == "Bart"
}
//instead of where.findAll()
List personsLocked = where.lockAll()

I know, I can achieve that by criteria API:

List personsLocked = Person.createCriteria().list {
    eq('firstName', 'Bart')
    lock true
}

Is there a way to achieve this via the "where" query ?


Solution

  • lock is not available in the grails.gorm.DetachedCriteria (result of where) and is only available from the org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder provided by createCriteira or by explicitly calling lock() on the instances and thus changing the LockMode to LockMode.UPGRADE

    You could always use the spread dot operator and lock the results after you get them.