vishesh
vishesh

Reputation: 2045

pessimistic locking: Lock database entity grails

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

Upvotes: 3

Views: 2398

Answers (1)

moskiteau
moskiteau

Reputation: 1102

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)    
}

Upvotes: -1

Related Questions