0

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 ?

Tomas Bartalos
  • 1,256
  • 12
  • 29

1 Answers1

2

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.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • 1
    By calling lock() on the returned instances, I would run into race condition (in the instant, when entity is read, but yet not locked). It seems that my only chance is to rewrite the query to criteria style. – Tomas Bartalos Sep 15 '14 at 00:22