4 Replies Latest reply on Aug 21, 2006 10:44 PM by confuz

    how to support treecache transaction in CMT

    confuz

      Dear All,
      I used TreeCache 1.4.0.GA and Jboss 4.0. currently need to support cache transaction in CMT. in the session bean method, the db operation will be rollbacked while cache operation won't when exception is threw.

      how to support cache transaction in cmt?

      Thanks for any help.

      code sample in session bean

      try{
       dboperation
       cacheoperation
      
       }catch(Exception e){
       sessionContext.setRollbackOnly();
       CCException ee = handleSQLException(e) ;
       throw ee;
       } finally {
       closeConnection(this.getClass().getName(), dbConn);
       }
      


      configuration
      <server>
      
       <classpath codebase="./lib" archives="jboss-cache.jar, jgroups.jar"/>
      
      
       <!-- ==================================================================== -->
       <!-- Defines TreeCache configuration -->
       <!-- ==================================================================== -->
      
       <mbean code="org.jboss.cache.TreeCache"
       name="jboss.cache:service=testTreeCache">
      
       <depends>jboss:service=Naming</depends>
       <depends>jboss:service=TransactionManager</depends>
       <attribute name="UseReplQueue">false</attribute>
      
       <attribute name="TransactionManagerLookupClass">org.jboss.cache.GenericTransactionManagerLookup</attribute>
      
       <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
      
       <attribute name="CacheMode">LOCAL</attribute>
      
       <attribute name="ClusterName">TreeCache-Cluster</attribute>
      
       <attribute name="ClusterConfig">
       <config>
       <UDP mcast_addr="228.1.2.5" mcast_port="45577"
       ip_ttl="64" ip_mcast="true"
       mcast_send_buf_size="150000" mcast_recv_buf_size="80000"
       ucast_send_buf_size="150000" ucast_recv_buf_size="80000"
       loopback="false"/>
       <PING timeout="2000" num_initial_members="3"
       up_thread="false" down_thread="false"/>
       <MERGE2 min_interval="10000" max_interval="20000"/>
       <FD shun="true" up_thread="true" down_thread="true"/>
       <VERIFY_SUSPECT timeout="1500"
       up_thread="false" down_thread="false"/>
       <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800"
       up_thread="false" down_thread="false"/>
       <pbcast.STABLE desired_avg_gossip="20000"
       up_thread="false" down_thread="false"/>
       <UNICAST timeout="600,1200,2400" window_size="100" min_threshold="10"
       down_thread="false"/>
       <FRAG frag_size="8192"
       down_thread="false" up_thread="false"/>
       <pbcast.GMS join_timeout="5000" join_retry_timeout="2000"
       shun="true" print_local_addr="true"/>
       <pbcast.STATE_TRANSFER up_thread="false" down_thread="false"/>
       </config>
       </attribute>
      
       <attribute name="InitialStateRetrievalTimeout">20000</attribute>
      
       <attribute name="SyncReplTimeout">15000</attribute>
      
       <attribute name="LockAcquisitionTimeout">10000</attribute>
      
      
       <attribute name="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute>
       <attribute name="EvictionPolicyConfig">
       <config>
       <attribute name="wakeUpIntervalSeconds">3</attribute>
       <!-- Cache wide default -->
       <region name="/_default_">
       <attribute name="maxNodes">5000</attribute>
       <attribute name="timeToLiveSeconds">1000</attribute>
       </region>
       <region name="/org/jboss/data">
       <attribute name="maxNodes">5000</attribute>
       <attribute name="timeToLiveSeconds">1000</attribute>
       </region>
       <region name="/test">
       <attribute name="maxNodes">500</attribute>
       <attribute name="timeToLiveSeconds">0</attribute>
       <attrubute name="maxAgeSeconds">0</attrubute>>
       </region>
       </config>
       </attribute>
       </mbean>
      
      
      </server>
      


        • 1. Re: how to support treecache transaction in CMT
          brian.stansberry

          That looks like it should work. The cache should be participating in the tx as a Synchronization, and when the tx rolls back, the cache modifications should roll back.

          What specifically do you do in "cacheoperation"?

          • 2. Re: how to support treecache transaction in CMT
            confuz

             

            "bstansberry@jboss.com" wrote:
            That looks like it should work. The cache should be participating in the tx as a Synchronization, and when the tx rolls back, the cache modifications should roll back.

            What specifically do you do in "cacheoperation"?


            the session bean method will invoke this method. forgive for the disorder code.
            
             public String checkAccess(String key1) throws Exception {
             String key = key1.substring(0,2);
             String value = key1;
             String command = key1.substring(2,5);
             TreeCache cache = CacheFactory.getCache();
            
            
             if("add".equals(command)){
             Address add = ((Person)cache.get("/test/p1","p1")).getAddress();
             add.setCity(value);
             cache.put("/test/a1","a1" ,add);
            
             if("pp".equals(key)){
             System.out.println("Gettttttttttttttbefore:a1=" + cache.get("/test/a1","a1"));
             throw new Exception("throw exception for rollback");
             }
            
             }
             else{
             boolean inCache = cache.exists("/test/" + key,key);
             boolean inLoader = false;//cache.getCacheLoader().exists(Fqn.fromString("/test/" + key));
             System.out.println("Exists in cache:" + inCache + " exists in loader:" + inLoader);
             Object o = (cache.get("/test/" + key,key));
             if(o != null)
             value = o.toString();
             System.out.println("Get:" + key + "=" + value);
             }
            
            
             System.out.println("Gettttttttttttttafter:a1=" + cache.get("/test/a1","a1"));
            
             return value;
             }
            

            CacheFactory.getCache()
            
             public static synchronized TreeCache getCache(){
             if(CACHE != null) return CACHE;
             initCache();
             return CACHE;
             }
            
            
             private static void initCache(){
             try{
             CACHE = new TreeCache();
             PropertyConfigurator pc = new PropertyConfigurator();
             pc.configure(CACHE, "tree-service.xml");
            
             CACHE.startService();
            
             Person p1 = new Person();
             p1.setName("P1");
             Person p2 = new Person();
             p2.setName("P2");
             Address add = new Address();
             add.setCity("SZ");
             add.setStreet("NR");
             add.setZip(3009884);
             p1.setAddress(add);
             p2.setAddress(add);
            
             CACHE.put("/test/p1","p1", p1);
             CACHE.put("/test/p2", "p2", p2);
             CACHE.put("/test/a1", "a1",add);
             }
             catch(Exception e){
             e.printStackTrace();
             }
             }
            


            • 3. Re: how to support treecache transaction in CMT
              brian.stansberry

              Was just about to say that looks fine too, when I saw the problem :)

              The fact that you put your Person object in the cache doesn't make the Person object transactional. You are changing the person object by changing the address -- that change won't roll back.

              To get what you want you'd need something more like:

              if("add".equals(command)){
               Person old = ((Person)cache.get("/test/p1","p1"));
               Person clone = (Person) old.clone();
               Address oldAdd = clone.getAddress();
               Address addClone = (Address) oldAdd.clone();
               addClone.setCity(value);
               clone.setAddress(addClone);
               cache.put("/test/a1","a1" ,clone);
              
               if("pp".equals(key)){
               System.out.println("Gettttttttttttttbefore:a1=" + cache.get("/test/a1","a1"));
               throw new Exception("throw exception for rollback");
               }
              
               }


              Usual warning that above code is for illustration, may not compile, etc.

              To have changes to your objects be transactional, you'd need to use PojoCache and instrument your classes.

              • 4. Re: how to support treecache transaction in CMT
                confuz

                Currently as my understand. the treecache only support two type transactions
                1. new instance putting can rollback.
                2. remove instance from cache can rollback.

                i think that is a limitation for treecache but can not found any explication about that in the treecache document.