In a Spring 3.1 MVC app with Hibernate 4.1, I am using:
- Declarative transaction management
- Session per request
- Optimistic locking by adding the “version” property in the domain objects
- Reattaching detached domain object using saveOrUpdate()
Domain object is sent as command object during a GET request in order to be updated by the user.
In order not to get the error “optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)”, i am including in each jsp page the “<form:hidden path="version"/>” and updates are working fine.
In order to further protect the app, i am trying to add to the controller the following:
@InitBinder
public void initBinder(WebDataBinder dataBinder, HttpServletRequest request) {
if (request.getMethod().equals("POST")) {
dataBinder.setDisallowedFields( new String[] {"version"});
}
}
But when i do that, i am getting the error optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)”.
Can someone please explain if what i am trying to do is valid as an approach and what i am doing wrong?
I believe ideally, i should try to protect and the primary key and perhaps some fields of the domain object that are not modified by the specific jsp page.
Thank you in advance.
This does not look correct, you should not disallow binding of version field – if you disallow the binding of version field, then your command object will not have the version field that you originally retrieved for the object from the DB, and at the point of persisting the command object hibernate would assume that the row has already been modified by some other transaction(as the version in the db and the command object does not match up).
You can try to protect the field(id, version) etc, only at the point of first save of the entity(when id and version are not expected to be present), but for an update the original id and version have to be present.