Let's say I have the following domain class:
class Book {
String name
// more properties here, but name is the only one relevant for this example
}
I'd like to update its name via a view. I'm using the following form to update it:
<g:form action="updateName" id="${book.id}">
<g:hiddenField name="version"/>
<g:textField name="name"/>
...
</g:form>
In the controller, I'm using the following logic:
def updateName() {
println("IN UPDATENAME()")
def book = Book.get(params.id)
println("VERSION BEFORE ASSIGN: ${book.version})
book.version = params.long('version')
println("VERSION AFTER ASSIGN: ${book.version})
book.name = params.name
book.save(flush: true)
...
}
I'm testing it by opening the same edit page in 2 different browsers. I do updateName
in one browser, followed by the other one. The second updateName
should be throwing an OptimisticLockingFailureException
, but it's not.
I enabled SQL output and this is what I get in the logs:
IN UPDATENAME()
VERSION BEFORE ASSIGN: 0
VERSION AFTER ASSIGN: 0
update book set version=?, name=? where id=? and version=?
binding parameter [1] as [BIGINT] - 1
binding parameter [2] as [STRING] - 'abc123'
binding parameter [3] as [BIGINT] - 1
binding parameter [4] as [BIGINT] - 0
IN UPDATENAME()
VERSION BEFORE ASSIGN: 1
VERSION AFTER ASSIGN: 0
update book set version=?, name=? where id=? and version=?
binding parameter [1] as [BIGINT] - 1
binding parameter [2] as [STRING] - 'def456'
binding parameter [3] as [BIGINT] - 1
binding parameter [4] as [BIGINT] - 1
In other words, in the second call, I was able to successfully assign the version from 1 to 0, which should have caused the exception, but for some reason, the SQL call still checks incorrectly for version = 1 instead of 0. Does anybody know why this is happening?