(Quick Reference)

lock

Purpose

The lock method obtains a pessimistic lock using an SQL select … for update.

Examples

def book = Book.get(1)
book.lock()

Description

The lock method obtains a pessimistic lock on an instance, locking the row in the database with select … for update. The lock method is equivalent to using Hibernate's LockMode.UPGRADE in combination with the lock method.

The lock is automatically released when the transaction commits. In Grails this is typically after an action has finished executing.

Refer to the section on Optimistic and Pessimistic locking in the user guide for info.