The lock is released when the transaction is completed, but not when save () is called, because save () is still saved after saving the transaction. A transaction can be committed when the execution of the code leaves the service method or when the sleep session is reset (after the HTTP request is completed, the session is cleared and cleared).
The best way to release the lock is to use manual transaction processing. In this case, do the following:
def saveUser(Long userId) { User.withNewTransaction { TransactionStatus status -> User user = User.lock(userId) user.save() } }
source share