Created
November 12, 2012 11:51
-
-
Save marcpalmer/4058938 to your computer and use it in GitHub Desktop.
Hibernate optimist locking code snippet #7
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
// Transactional service method | |
void addComment(Photo photo, String text) { | |
def comment = new PhotoComment(text: text) | |
comment.save() | |
// We have to lock the parent here, or we will not prevent | |
// background task changing things (as it happens the | |
// changes do not overlap…) | |
photo.lock() | |
photo.addToComments(comment) | |
} | |
// Asynchronous Quartz Job method | |
void uploadPhoto(Photo p) { | |
// do something with S3 | |
// … | |
// Create a new transaction so we don't hog the lock for | |
// too long | |
Photo.withTransaction { txn -> | |
def photo = Photo.get(p.id) | |
photo.lock() | |
photo.url = newUrls | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment