Description
David Prevost opened DATACOUCH-623 and commented
I wanted to use @Version
(with long version) to use optimistic locking and kick the CAS of couchbase like mentioned in the documentation.
However, after doing an integrated test, I found out that neither the CASMismatchException or the OptimisticLocking exception are thrown. The problem seems pretty much similar to https://jira.spring.io/browse/DATACOUCH-212.
After searching a bit, it seems that if we want CAS to work, we must use couchbaseOperations.replace... However, the current implement of repositories seems to be using upsert. I was not able to test this theory though.
My integrate test was basically doing:
- Create a User
- Update the token to add one token then save
- Build a new User object with a previous version and a new token and the same id
- Save
- Expecting Optimitics lock error but it is save without error
public void givenExistingUser_whenChangeByTwoPersonsAndSaved_thenOptimisticError() {
// Given
final User savedUser = givenExistingUser();
// When
// First person
savedUser.setTokenIds(Set.of(UUID.randomUUID()));
userSessionDao.save(savedUser);
// Second person
final User savedUser2 =
UserBuilder.builder().userUuid(savedUser.getUserUuid()).build();
savedUser2.setVersion(savedUser.getVersion() - 1);
savedUser2.settokenIds(Set.of(UUID.randomUUID()));
// Then
final Throwable throwable = catchThrowable(() -> userSessionDao.save(savedUser2));
assertThat(throwable).isExactlyInstanceOf(CasMismatchException.class);
}
@Document
public class UserEntity {
@Id
private UUID userUuid;
@Version
private long version;
@Field
@NotNull
private Set<UUID> tokenIds;
Affects: 4.0.4 (Neumann SR4)
Referenced from: pull request #268
Backported to: 4.0.5 (Neumann SR5)
1 votes, 4 watchers