Uploaded image for project: 'Spring Data Couchbase'
  1. Spring Data Couchbase
  2. DATACOUCH-623

CAS not working with @Version

    XMLWordPrintable

    Details

      Description

      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;
      

       

        Attachments

          Activity

            People

            Assignee:
            mikereiche Michael Reiche
            Reporter:
            plum117 David Prevost
            Last updater:
            Spring Issues Spring Issues
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: