Skip to content

Instantly share code, notes, and snippets.

@marcpalmer
Created November 12, 2012 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcpalmer/4058938 to your computer and use it in GitHub Desktop.
Save marcpalmer/4058938 to your computer and use it in GitHub Desktop.
Hibernate optimist locking code snippet #7
// 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