1 Reply Latest reply on Mar 12, 2007 3:02 PM by manik

    Optimistic locking behaviour not consistent with pessimistic

      JBoss Cache version: 1.4.1.SP2

      This relates to an issue I am having with Hibernate:

      http://forum.hibernate.org/viewtopic.php?p=2343598

      When I'm using pessimistic locking, the following code will work:

      public static void main(String[] args) throws Exception {
       UserTransaction tx= new DummyUserTransaction(DummyTransactionManager.getInstance());
       TreeCache tree = new TreeCache();
       PropertyConfigurator config = new PropertyConfigurator();
       config.configure(tree, "treecache.xml");
       tree.startService(); // kick start tree cache
      
       try {
       tx.begin();
       tree.put (fqn("/parent/child"), "item", 1 ,option(true));
       tree.remove(fqn("/parent"), option(false));
       tree.remove (fqn("/parent/child"), option(false));
       tx.commit();
       }
       catch(Throwable ex) {
       ex.printStackTrace();
       try { tx.rollback(); } catch(Throwable t) {}
       } finally{
       tree.stopService();
       }
       }
      
       private static Fqn fqn(String s){
       return Fqn.fromString(s);
       }
      
       private static Option option(boolean failSilently){
       Option option = new Option();
       option.setFailSilently( failSilently );
       option.setDataVersion(new DataVersion(){
      
       public boolean newerThan(DataVersion arg0) {
       return false;
       }} );
       return option;
       }


      But with optimistic locking, the last remove call will cause the following exception:
      org.jboss.cache.CacheException: Unable to find parent node with Fqn /parent
       at org.jboss.cache.interceptors.OptimisticNodeInterceptor.removeNode(OptimisticNodeInterceptor.java:218)
       at org.jboss.cache.interceptors.OptimisticNodeInterceptor.invoke(OptimisticNodeInterceptor.java:110)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.EvictionInterceptor.invoke(EvictionInterceptor.java:88)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor.invoke(OptimisticCreateIfNotExistsInterceptor.java:69)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.OptimisticValidatorInterceptor.invoke(OptimisticValidatorInterceptor.java:84)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.OptimisticLockingInterceptor.invoke(OptimisticLockingInterceptor.java:126)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.TxInterceptor.handleNonTxMethod(TxInterceptor.java:365)
       at org.jboss.cache.interceptors.TxInterceptor.invoke(TxInterceptor.java:160)
       at org.jboss.cache.interceptors.Interceptor.invoke(Interceptor.java:68)
       at org.jboss.cache.interceptors.CacheMgmtInterceptor.invoke(CacheMgmtInterceptor.java:183)
       at org.jboss.cache.TreeCache.invokeMethod(TreeCache.java:5776)
       at org.jboss.cache.TreeCache.remove(TreeCache.java:3855)
       at org.jboss.cache.TreeCache.remove(TreeCache.java:3438)
       at com.medq.test.TestTreeCache.main(TestTreeCache.java:48)
      


      Is it just a fluke that it works with pessimistic locking, or is optimistic locking not behaving as it should?

      Note: The code above reproduces what Hibernate does when it evicts entities.