Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Created February 17, 2009 08:40
Show Gist options
  • Save masanobuimai/65645 to your computer and use it in GitHub Desktop.
Save masanobuimai/65645 to your computer and use it in GitHub Desktop.
Grails:楽観ロックのサンプル
class Person {
String name
Long lock_version
static transients = [ 'lock_version' ]
static constraints = {
version validator: { v, o -> !o.lock_version ?: o.lock_version == v }
}
}
<g:form method="post">
<input type="hidden" name="id" value="${personInstance?.id}"/>
<input type="hidden" name="version" value="${personInstance?.version}"/>
def update = {
def personInstance = Person.get(params.id)
if (personInstance) {
personInstance.properties = params
if (!personInstance.hasErrors() && personInstance.save()) {
flash.message = "Person ${params.id} updated"
redirect(action: show, id: personInstance.id)
}
else {
render(view: 'edit', model: [personInstance: personInstance])
}
}
else {
flash.message = "Person not found with id ${params.id}"
redirect(action: edit, id: params.id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment