3 Replies Latest reply on Aug 21, 2007 6:58 PM by fatbatman

    PojoCache - increment global counter

      Hello,

      I'm trying to use PojoCache1.4 to share a share a global counter across a cluster of servers. Something like this.

      @PojoCacheable
      public class GlobalValues implements Serializable {
      protected int globalCounter=0;

      public synchronized int incrementGlobalCounter(){
      globalCounter++;
      return globalCounter;
      }
      }


      incrementGlobalCounter() must always return the next value in the sequence even if called from two seperate machines in the cluster simulataneously. How do I do this with PojoCache? I've tried a number of things including setting the IsolationLevel to SERIALIZABLE, none of which work.

      What should I do?

      thanks in advance

      James


        • 1. Re: PojoCache - increment global counter

          Anyone?

          or is this not what PojoCache was intended for?

          • 2. Re: PojoCache - increment global counter
            jason.greene

            In order for this use case to be truly atomic, you need a distributed cluster-wide lock. JBoss Cache does not yet support an option for distributing locking (instead locks are acquired when applying remote transaction batches), mainly because distributed locking does not scale very well, and is overkill for most use cases. However, it is on the roadmap. Please vote on it if this is important to you so that we understand the demand and can plan accordingly.

            http://jira.jboss.com/jira/browse/JBCACHE-1098

            In the meantime you have some other options:


            1. If your cluster shares a DB already, then use it's native sequence, or if it doesn't have one use "select for update" to serialize access to a column
            2. Ensure that only one node is used for this operation. If that node fails, you can then failover to another node, and ensure that all subsequent operations are against this node.
            3. Use the jboss HA Singleton service (this however would be vulnerable to cluster splits [each splitoff would end up with its own singleton until the cluster heals)
            4. Use jgroups to write your own sequence protocol. This wouldn't be too complex. You would be able to handle the split condition by incrementing the counter by some large amount when the coordinator changes, and then reconcile the counter state on merge


              -Jason


            • 3. Re: PojoCache - increment global counter

              Hi Jason,

              Thanks for your response, I voted for it.

              I'll just use a native database sequence for now.

              Keep up the great work on JBoss/PojoCache it's a really great product. It feels almost like magic when an object is automatically updated across the cluster :)

              many thanks

              James