3 Replies Latest reply on Sep 3, 2018 10:13 AM by Radim Vansa

    Inifinispan : add pessimistic read lock

    Amine Zaghdoudi Newbie

      Hello,

      in the documentation of Infinispan 9.3 i can only find that Infinispan supports the isolation Levels READ_COMMITTED and REPEATABLE_READ, which never blocks reads.

      is there any possibility to add / configure a pessimistic locks on reads in Infinispan ?

      Thanks,

        • 1. Re: Inifinispan : add pessimistic read lock
          Radim Vansa Master

          Short answer: No.

           

          Long answer: In pessimistic tx cache you can do cache.getAdvancedCache().withFlags(Flag.FORCE_WRITE_LOCK) but that won't work as you'd expect. Since reads don't visit all owners (these are satisfied by any owner) this would lock just some nodes and you could be in trouble. It would work only in distributed mode with single owner (or at least I *think* it would work).

          If you really need to lock the entry, you could read it and conditionally replace it (and verify that the replace succeeded). Writes will acquire those locks. Or you can go for optimistic locking and let the transaction fail with write skew if the entry gets changed.

           

          Could you describe your use case?

          • 2. Re: Inifinispan : add pessimistic read lock
            Amine Zaghdoudi Newbie

            Hi Radim

             

            Thank you for your response,

            we need to ensure that a entry could only get once, so kind of "read lock".

            • 3. Re: Inifinispan : add pessimistic read lock
              Radim Vansa Master

              In transaction with REPEATABLE_READ isolation level (default since Infinispan 9 or so) you store all read entries in a transaction context (imagine 1st level cache/session cache in JPA world), therefore you couldn't read two different values in one transaction even if it's modified by another transaction in the meantime.