-
Notifications
You must be signed in to change notification settings - Fork 356
DATAJDBC-219 - add support for optimistic locking #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DATAJDBC-219 - add support for optimistic locking #124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Tom,
Thanks for tackling this.
I think we can replace the VersionsAccessor
with to simple methods as in Mongo Db: https://github.com/spring-projects/spring-data-mongodb/blob/8f4db5540d70ddf4926824d050c50f73a35c6aab/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java#L672
These will also take care of handling immutable Version attributes properly.
A delete where an entity is given as a parameter should also check for optimistic locking.
I rebased and squashed your changes into a branch: https://github.com/spring-projects/spring-data-jdbc/tree/pr/datajdbc-219
Please use that for further work on this pr.
We try to avoid merge commits, since they make it hard to understand the changes made.
} | ||
|
||
String getUpdateWithVersion(Number version) { | ||
return String.format("%s AND %s = %s", updateSql.get(), entity.getVersionProperty().getColumnName(), version); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The versions should be a bind parameter as all other parameters.
For the parameter name pick something that is highly unlikely to collide with any other parameter names, like ___oldOptimisticLockingVersion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
af1a414
to
f60919b
Compare
f60919b
to
67eeca8
Compare
Hi @schauder, I was wondering if there was a simpler way for accessing and modifying the version field. I simplified it and removed the To support optimistic locking for the delete operation, I think we would have to modify |
Great, I'll take a look.
I don't think we need the full entity, but just the version attribute. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this works with an immutable version attribute.
Take a look how this is handled for the Id in https://github.com/spring-projects/spring-data-jdbc/blob/ade3324e2c8f578ca256f920998d76bd62a04df2/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java#L66
} | ||
|
||
private <T> Number getNextVersion(T instance, RelationalPersistentEntity<T> entity, ConversionService conversionService) { | ||
Number version = getVersion(instance, entity, conversionService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this fail if we just use always Long
? I'd have thought that it would simply work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if I use a ConvertingPropertyAccessor
, it automatically converts from Long to the required type. The next push will contain a simplified version.
And no, it currently does not work with an immutable version field. I'll add a test. I see the merit in having an immutable version field, but I fail to see how the code in AggregateChange
handles modifying an immutable ID field. It uses a PropertyAccessor
, too, doesn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trick is that the PropertyAccessor
creates a new bean with the new value for id/version and everything after that uses the new bean returned by propertyAccessor.getBean()
. See the end of the method I linked to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I get that. But in my case it fails even earlier with an UnsupportedOperationException
right here: https://github.com/spring-projects/spring-data-commons/blob/master/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java#L85
BeanWrapper
seems to expect a wither method. Is the existence of a wither method a valid constraint for our use case?
@schauder as for supporting optimistic locking for delete: Wouldn't it be cleaner to pass the whole entity through from And where does the public API start? I wasn't aware that any of this code is public API. |
@thombergs symmetry is a good argument (especially when talking to a physicist). Anything that is public (i.e. public methods in public classes) are considered public API by Spring. |
Yes, if an attribute is immutable we require a wither. |
@schauder The "don't touch public API" policy is hard on refactorings like this one where we have to pass through the whole stack :). Is it OK to modify the semantics of |
Don't we need both implementations anyway since deleting by id only should still be supported? Of course, both implementations should reuse most of the code |
@thombergs Sorry, missed the question about the formatters. See https://github.com/spring-projects/spring-data-build/tree/master/etc/ide |
Hi, I'm changing my application from Spring Data JPA to Spring Data JDBC and we use @Version for optimistic locking. |
This is certainly one of the more pressing issues but I can't really give any dates. If someone would rebase the PR on the current master and/or address the open issues layed out above that would certainly help. |
@schauder I have taken the work Tom started, rebased, and I have the update/insert working. I am going to look at the delete next. I am not yet ready for a PR, but the branch is here : https://github.com/tkvangorder/spring-data-jdbc/tree/DATAJDBC-219 |
There is a new PR, building on this one and replacing it: #166 |
Reduce Lombok usage to within tests.
Tighten nullability constraints. Consistent Javadoc.
This PR adds support for optimistic locking by evaluating the
@Version
annotation.Some indentations are wrong as I didn't find a code formatter configuration but I can fix indentations if you point me to your code formatter.