1 Reply Latest reply on Sep 26, 2006 12:41 PM by brian.stansberry

    TreeCache: How to write atomic updates?

    kapilanand

      hi all
      I am trying to do the following from two or more hosts that are part of a JBossCache group. (I am using JBossCache 1.4)

      If exists(FQN, key)
      {
      lock FQN
      value = get(FQN, key)
      value--
      if (value == 0)
      {
      remove(FQN, key)
      }
      else
      {
      put(FQN, key, value)
      }
      unlock FQN
      }

      There is another thread of code that will increment the value. increment and decrement may happen on any of the hosts.

      My intention is only to lock the map and i guess that is only achieved by locking the FQN node. Please correct me if i am wrong.

      My question is "how to explicitly lock and unlock the nodes?"
      Javadoc for TreeCache._lock() has one-liner saying "Should not be called." I see _releaseAllLocks() and releaseAllLocks().

      And is there a mechanism for timing-out the lock. That is what happens if the process/thread that locked the node dies?

      thanks

        • 1. Re: TreeCache: How to write atomic updates?
          brian.stansberry

          There is no explicit lock/unlock API. To get the effect you want you should use a transaction; a RL will be acquired on the get() call; WL for the put/remove. Locks will be retained until the tx commits or rolls back. If for some reason the calling thread hangs, eventually the tx will time out adn the transaction manager will roll it back, releasing the locks.

          Please note though that a call to exists() does not acquire any lock.