0 Replies Latest reply: Oct 23, 2013 7:09 AM by AustinChen RSS

    the cachestore 's storemethod invokes times related with the number of nodes?

    AustinChen
      /**
      *
      */
      package com.baoxian.coherence.support;
      
      
      /**
      * @author AustinChen
      * 缓存对象,拥有一个返回cache的key的方法
      */
      public interface ICacheObject {
         
          Object getCacheKey();
      
      
      }
      
      

       

      import java.io.Serializable;
      
      
      import com.baoxian.coherence.support.ICacheObject;
      
      
      /**
      *
      */
      
      
      /**
      * @author AustinChen
      *
      */
      public class User2 implements Serializable,ICacheObject {
      
      
          private int id;
          public int getId() {
              return id;
          }
      
      
          public void setId(int id) {
              this.id = id;
          }
      
      
          public String getName() {
              return name;
          }
      
      
          public void setName(String name) {
              this.name = name;
          }
         
          private String name2;
      
      
          public String getName2() {
              return name2;
          }
      
      
          public void setName2(String name2) {
              this.name2 = name2;
          }
      
      
          private String name;
         
          @Override
        public String toString() {
        return "User [id=" + id + ", name=" + name
        +",name2="+name2+ "]";
        }
      
      
          @Override
          public Object getCacheKey() {
        return id;
          }
      }
      
      

       

      import java.util.Collection;
      import java.util.Map;
      import java.util.Map.Entry;
      import java.util.Set;
      
      
      import com.tangosol.net.cache.CacheStore;
      
      
      /**
      *
      */
      
      
      
      
      /**
      * @author AustinChen
      *
      */
      public class User2JdbcCacheStore extends User2JdbcCacheLoader implements CacheStore {
         
         
      
      
         
          public User2JdbcCacheStore() {
      
        // TODO Auto-generated constructor stub
          }
          public User2JdbcCacheStore(String driverClassName, String url,
           String userName, String password) {
      
        super(driverClassName, url, userName, password);
        System.out.println("i'm a new instance");
        // TODO Auto-generated constructor stub
          }
      
      
          /*
           * (non-Javadoc)
           *
           * @see com.tangosol.net.cache.CacheStore#erase(java.lang.Object)
           */
          public void erase(Object key) {
        System.out.println("threadid:"+Thread.currentThread().getId()+",erase key:"+key);
        String deleteSql = "delete from user2 where id=?";
      
        this.saveOrUpdate(deleteSql, key);
      
      
          }
      
      
          public void eraseAll(Collection keys) {
        System.out.println("threadid:"+Thread.currentThread().getId()+",erase all size:"+keys.size());
        //if
        //String deleteSql="delete from user2 where id in(";
      
      
        // this.saveOrUpdate(deleteSql);
      
      
        //throw new java.lang.IllegalArgumentException("无法执行全部删除数据库动作!");
      
      
          }
         
          public void storeUser(Object key,Object user) throws Exception
          {
        String sql;
        User2 user2 = (User2) user;
        if (load(key) != null) {
          
         
           // key exists - update
           sql = "UPDATE user2 SET name = ?,name2=? where id = ?";
           int result=-1;
           //while(result>0)
          //{
           result=this.saveOrUpdate(sql, user2.getName(),user2.getName2(), user2.getId());
      //    if(result<0)
      //    {
      // try {
      // Thread.currentThread().sleep(2000);
      //    } catch (InterruptedException e) {
      // // TODO Auto-generated catch block
      // //e.printStackTrace();
      //    }
      // continue;
      //    }
      //    else
      //
      //    {
      // break;
      //    }
      //   
      //    }
           updateTimes++;
           log.info("Update Object times:"+updateTimes);
        } else {
          
           // new key - insert
           sql = "INSERT INTO user2 (name,name2) VALUES (?,?)";
           this.saveOrUpdate(sql, user2.getName(),user2.getName2());
           insertTimes++;
           log.info("Insert Object times:"+insertTimes);
        }
          }
      
      
          public void store(Object key, Object user) {
      
        System.out.println("threadid:"+Thread.currentThread().getId()+",开始执行store,key:"+key+",value:"+user);
      
        if(isNeedTraceExceptionStack)
          
        new Exception("store").printStackTrace();
        try {
           storeUser(key,user);
        } catch (Exception e) {
           throw  ensureRuntimeException(e,"store failed!!!");
        }
      
      
          }
      
      
          @SuppressWarnings("unchecked")
          public void storeAll(Map users) {
        System.out.println("threadid:"+Thread.currentThread().getId()+",开始执行StoreAll,size:"+users.size());
        for(Entry<Object, Object> entry :(Set<Map.Entry<Object, Object>>)users.entrySet())
        {
           try {
        storeUser(entry.getKey(),entry.getValue());
           } catch (Exception e) {
        throw  ensureRuntimeException(e,"store failed!!!");
           }
        }
        //throw new java.lang.IllegalArgumentException("暂不提供批量保存功能!");
          }
      
      
      }
      
      
      

       

      my cache config file

      <?xml version="1.0"?>
      
      
      <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
        xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config
         coherence-cache-config.xsd">
        <caching-scheme-mapping>
        <cache-mapping>
        <cache-name>*</cache-name>
        <scheme-name>distributed</scheme-name>
        </cache-mapping>
        <cache-mapping>
        <cache-name>User2</cache-name>
        <scheme-name>distributed-User2</scheme-name>
              </cache-mapping>
        </caching-scheme-mapping>
      
      
        <caching-schemes>
        <distributed-scheme>
        <scheme-name>distributed-User2</scheme-name>
        <backing-map-scheme>
        <read-write-backing-map-scheme>
      
        <internal-cache-scheme>
        <local-scheme>
        <high-units>{back-size-limit 0}</high-units>
        <expiry-delay>{back-expiry 0}</expiry-delay>
        </local-scheme>
        </internal-cache-scheme>
        <cachestore-scheme>
        <class-scheme>
        <class-name>User2JdbcCacheStore</class-name>
        <init-params>
        <init-param>
        <param-type>java.lang.String</param-type>
        <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
        <init-param>
        <param-type>java.lang.String</param-type>
        <param-value>jdbc:mysql://127.0.0.1:3306/coherencetest</param-value>
        </init-param>
        <init-param>
        <param-type>java.lang.String</param-type>
        <param-value>root</param-value>
        </init-param>
        <init-param>
        <param-type>java.lang.String</param-type>
        <param-value>123456.cc</param-value>
        </init-param>
        </init-params>
        </class-scheme>
        </cachestore-scheme>
        <write-delay>10s</write-delay>
        <write-requeue-threshold>100</write-requeue-threshold>
        <refresh-ahead-factor>1.0</refresh-ahead-factor>
        </read-write-backing-map-scheme>
        </backing-map-scheme>
        </distributed-scheme>
        <distributed-scheme>
        <scheme-name>distributed</scheme-name>
        <service-name>DistributedCache</service-name>
        <backing-map-scheme>
        <local-scheme>
        <high-units>0</high-units>
        <expiry-delay>0</expiry-delay>
        </local-scheme>
        </backing-map-scheme>
        <autostart>true</autostart>
        </distributed-scheme>
        </caching-schemes>
      </cache-config>
      
      

       

      my invoke code for example:

      import com.tangosol.net.CacheFactory;
      import com.tangosol.net.NamedCache;
      
      
      /**
      *
      */
      
      
      /**
      * @author AustinChen
      *
      */
      public class TestInserUser2 {
      
      
          /**
           * @param args
           */
          public static void main(String[] args) {
        CacheFactory.ensureCluster();
        String cachename = "User2";
        NamedCache cache =CacheFactory.getCache(cachename);
        for (int i = 11; i < 20011; i++) {
        User2 user=new User2();
        user.setId(100+i);
        user.setName("initname-"+i);
        user.setName2("initname2-"+i);
        cache.put(user.getId(), user);
              //Thread.sleep(500);
        }
      
      
          }
      
      
      }
      
      

       

      when i create one node then then CacheStore store method only been called 8723 times;

       

      it invoked 13420 times at two nodes.

       

      which cause the problem?

       

      could anyone help me?