7.5.2.7 Optimistic Locking and Versioning - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.3
7.5.2.7 Optimistic Locking and Versioning
As discussed in the section on Optimistic and Pessimistic Locking, by default GORM uses optimistic locking and automatically injects aversion
property into every class which is in turn mapped to a version
column at the database level.If you're mapping to a legacy schema that doesn't have version columns (or there's some other reason why you don't want/need this feature) you can disable this with the version
method:class Person { … static mapping = { table 'people' version false } }
If you disable optimistic locking you are essentially on your own with regards to concurrent updates and are open to the risk of users losing data (due to data overriding) unless you use pessimistic locking
Version columns types
By default Grails maps theversion
property as a Long
that gets incremented by one each time an instance is updated. But Hibernate also supports using a Timestamp
, for example:import java.sql.Timestampclass Person { … Timestamp version static mapping = { table 'people' } }
Timestamp
instead of a Long
is that you combine the optimistic locking and last-updated semantics into a single column.