Created
November 12, 2012 11:54
-
-
Save marcpalmer/4058953 to your computer and use it in GitHub Desktop.
Hibernate optimist locking code snippet #12
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) | |
// Discover we do need to make changes to the main object | |
try { | |
object.lock() | |
} catch (Exception e) { | |
// Recover from staleness | |
// Reload locked… | |
object = MyDomain.lock(params.id) | |
// Double check - do we still want to change it now? | |
} | |
// Make some changes | |
object.property1 = 'hello' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment