0 Replies Latest reply on Dec 13, 2011 10:20 AM by meenarajani

    Infinispan 5 Invalidation.Sync

    meenarajani

      Hi I have two questions.

       

      I want to see how infinispan behaves in Invalidation mode.  I am putting same values with same keys on two different cache nodes.

      the later put command in second node result in invalidation of data in first node though the data value and key is same. So how do I avoid that? I want to cache node 2 to not to invalidate or recognise that the data in the node1 is same hence not invalidate it.

      I just do not want to use the jboss right now just want to  get a feeling of how cache works without jboss part.

       

      My other question is that how data in transaction is affected if someone trying to modify the same data, in the same cache node where the current transaction is runing, or in some other cache node.

       

       

       

      import org.apache.log4j.PropertyConfigurator;

      import org.infinispan.Cache;

      import org.infinispan.config.Configuration;

      import org.infinispan.config.GlobalConfiguration;

      import org.infinispan.manager.DefaultCacheManager;

      import org.infinispan.manager.EmbeddedCacheManager;

      import org.testng.annotations.Test;

       

      import javax.transaction.HeuristicMixedException;

      import javax.transaction.HeuristicRollbackException;

      import javax.transaction.NotSupportedException;

      import javax.transaction.RollbackException;

      import javax.transaction.SystemException;

      import javax.transaction.TransactionManager;

      import org.infinispan.transaction.lookup.GenericTransactionManagerLookup;

       

       

       

      import org.apache.log4j.PropertyConfigurator;

      import org.infinispan.Cache;

      import org.infinispan.config.Configuration;

      import org.infinispan.config.GlobalConfiguration;

      import org.infinispan.manager.DefaultCacheManager;

      import org.infinispan.manager.EmbeddedCacheManager;

      import org.testng.annotations.Test;

       

      import javax.transaction.HeuristicMixedException;

      import javax.transaction.HeuristicRollbackException;

      import javax.transaction.NotSupportedException;

      import javax.transaction.RollbackException;

      import javax.transaction.SystemException;

      import javax.transaction.TransactionManager;

      import org.infinispan.transaction.lookup.GenericTransactionManagerLookup;

       

       

       

      public class cacheTest6 {

           public static void main(String[] args) throws NotSupportedException, Exception  {

           PropertyConfigurator.configure("log4j.properties");

       

            

           Configuration cfg = new Configuration();

           cfg.setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC);

           GenericTransactionManagerLookup myTrans = new GenericTransactionManagerLookup();

           cfg.fluent().transactionManagerLookup(myTrans);

       

           EmbeddedCacheManager cm1 = new DefaultCacheManager(GlobalConfiguration.getClusteredDefault(), cfg);

           Cache<Integer,Integer> cache1 = cm1.getCache();

           TransactionManager tm1 = cache1.getAdvancedCache().getTransactionManager();

       

           EmbeddedCacheManager cm2 = new DefaultCacheManager(GlobalConfiguration.getClusteredDefault(), cfg);

           Cache<Integer,Integer > cache2 = cm2.getCache();

           TransactionManager tm2 = cache2.getAdvancedCache().getTransactionManager();

           System.out.println(" before putting  1, test in Cache" );

          

           //There two cache nodes cahe1 and cache 2. I want to see how cache behaves when i put some thing in two ndes. eg

           // i am putting same data in both nodes expecting that the cache node2 will just put the (1,11) in cache2

           // on the contrary when I am putting cache2.put(1, 11) the data in cache1 is invalidated and when i do cache2.get(1) I get null.

           cache1.put(1, 11);

           cache2.put(1, 11);

         

           System.out.println(cache1.get(1)+" cache1.get(1) "); // getting null

           System.out.println(cache2.get(1)+" cache2.get(1) ");

        

           cache1.put(1, 12); // this should invalidate and it does

           System.out.println(cache2.get(1)+"   cache2.get(1) after  cache1.put(1, 12)");

           System.out.println("  after changing value of  1, " );

         tm2.begin();

              cache2.get(1);

              MyRunnable task = new MyRunnable();

              Thread worker = new Thread(task);

              worker.setName("thread1");

              worker.start();

              System.out.println(cache2.get(1)+"   cache2.get(1) This is from Transaction");

         tm2.commit();

         if (worker.isAlive()){

             cache1.put(1,10000);

             System.out.println(cache2.get(1)+"   cache2.get(1) This is outside Transaction");

             System.out.println(cache1.get(1)+ "   cache1.get(1) This is outside Transaction");

         }

           System.out.println(cache2.containsKey(1) + "after change has been done on cache1 node" );

           System.out.println(cache2.get(1) + "after change has been done on cache1 node");

           cache1.put(2, 22);

           System.out.println(" after putting  2, test in Cache" );

         

         

         

        }

       

         

      }