pessimistic locking in GORM where query

2019-04-17 09:55发布

问题:

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 ?

回答1:

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.