3

I followed the grails documenation it says that to do pessimistic locking I can do like this:

def plan = Plan.findWhere(user:user, [lock: true])

so this locks the plan instance until save is finished on it.Now in my case I want to lock multiple plans at once,like this:

def plan = Plan.findAllWhere(user:user, [lock: true])

I am doing this in grails service which are transactional by default,but the above line is not working as expected.It doesn't lock all the rows and throws stale state exception if a concurrent transaction is performed.

How can I lock multiple rows when reading?

Please see a related question for more info : concurrent transaction in grails resulting in database stale state exception

Community
  • 1
  • 1
vishesh
  • 2,007
  • 6
  • 32
  • 67

1 Answers1

-1

You can do something like this, which works for me:

Process.withNewTransaction {
    def process = Process.get(job.process.id)
    process.lock()
    process.sessionId = 0
    process.processId = 0
    process.save(flush:true)    
}
krock
  • 28,904
  • 13
  • 79
  • 85
moskiteau
  • 1,104
  • 11
  • 19
  • this works for you,because you only get one row by doing `Process.get(job.process.id)`.Please see the linked question that details why I can't use a solution like yours.Thanks – vishesh Nov 22 '13 at 14:52