Created
November 12, 2012 11:55
-
-
Save marcpalmer/4058954 to your computer and use it in GitHub Desktop.
Hibernate optimist locking code snippet #13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use any domain class | |
MyDomain.withTransaction { txn -> | |
// Load objects | |
def object = MyDomain.get(params.id) | |
if (object.type == 'whatever') { | |
// Discover we do need to make changes to the main object | |
// Just discard what we had, we haven't changed it | |
// and get the new locked version | |
object = MyDomain.lock(params.id) | |
// Again, the decisions we made about changing it may be | |
// dependent on values we just refreshed from the DB | |
// So check it again | |
if (object.type == 'whatever') { | |
// Make some changes | |
object.property1 = 'hello' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment