0 Replies Latest reply on Oct 28, 2006 4:38 AM by Kresimir Tonkovic

    Optimistic locking with Hibernate and JCache

    Kresimir Tonkovic Newbie

      Hibernate 3.1, Jboss 4.0.3SP1

      I'm trying to get optimistic locking to work, but with no luck.

      I'm using Jboss cache 1.4. Configuration of cache and session factory:

      <server>
       <mbean code="org.jboss.cache.TreeCache" name="jboss.har:service=HarSecondLevelCache">
       <attribute name="CacheMode">LOCAL</attribute>
       <attribute name="TransactionManagerLookupClass">org.jboss.cache.JBossTransactionManagerLookup</attribute>
       <attribute name="NodeLockingScheme">OPTIMISTIC</attribute>
       <attribute name="EvictionPolicyConfig">
       <config>
       <attribute name="wakeUpIntervalSeconds">5</attribute>
       <!-- Cache wide default -->
       <region name="/_default_">
       <attribute name="maxNodes">50000</attribute>
       <attribute name="timeToLiveSeconds">1000</attribute>
       <!-- Maximum time an object is kept in cache regardless of idle time -->
       <attribute name="maxAgeSeconds">600</attribute>
       </region>
       </config>
       </attribute>
       </mbean>
       <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Chip_HB_LOCAL">
       <attribute name="DatasourceName">java:/jdbc/ChipDS_LOCAL</attribute>
       <attribute name="Dialect">org.hibernate.dialect.PostgreSQLDialect</attribute>
       <attribute name="SessionFactoryName">java:/hibernate/ChipHB_LOCAL</attribute>
       <attribute name="CacheProviderClass">org.jboss.hibernate.cache.DeployedTreeCacheProvider</attribute>
       <attribute name="DeployedTreeCacheObjectName">jboss.har:service=HarSecondLevelCache</attribute>
       <attribute name="ReflectionOptimizationEnabled">false</attribute>
       <attribute name="MaxFetchDepth">1</attribute>
       <attribute name="ScanForMappingsEnabled">true</attribute>
       <attribute name="StatGenerationEnabled">true</attribute>
       <attribute name="ShowSqlEnabled">false</attribute>
       <!--attribute name="Hbm2ddlAuto">update</attribute-->
       </mbean>
      </server>
      


      Here is my class that I expect to be optimistically locked:
      /*
       * $Id$
       */
      package hr.chipoteka.erp.data;
      
      import java.util.Date;
      
      /**
       * @hibernate.class lazy = "false" dynamic-update = "true"
       */
      public abstract class Analitika extends DataObject {
      ...
      
       /**
       * @hibernate.version type = "timestamp"
       */
       public Date getTs() {
       return ts;
       }
      
       public void setTs(Date ts) {
       this.ts = ts;
       }
      }


      and one subclass:

      package hr.chipoteka.erp.data.artikl;
      
      import hr.chipoteka.erp.data.Analitika;
      import hr.chipoteka.erp.data.SlimDataObject;
      import hr.chipoteka.erp.data.osoba.PoslovniPartner;
      import hr.chipoteka.erp.data.osoba.adresa.Drzava;
      
      import java.util.*;
      
      /**
       * @hibernate.union-subclass lazy = "false" dynamic-update = "true"
       */
      public class Artikl extends Analitika implements SlimDataObject {
      ...
      }


      Generated hbm.xml looks like this:
      ?xml version="1.0" encoding="UTF-8"?>
      
      <!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      
      <hibernate-mapping
      
      >
      
       <class
       name="hr.chipoteka.erp.data.Analitika"
       lazy="false"
       dynamic-update="true"
       >
      
      ...
      
       <version
       name="ts"
       column="ts"
       type="timestamp"
       />
      
      ...
      
       <union-subclass
       name="hr.chipoteka.erp.data.artikl.Artikl"
       dynamic-update="true"
       lazy="false"
       >
      
      ...
      
       </union-subclass>
      ...
       </class>
      
      </hibernate-mapping>
      


      I use hibernate within stateless session beans.

      When running some HQL queries, I stop jboss at a breakpoint and check locks in the database (postgres):


      select pg_class.relname,pg_locks.locktype, page, tuple, transaction, pid, mode, granted from pg_class,pg_locks where pg_class.relfilenode=pg_locks.relation and pid = 31761 order by relname;
       relname | locktype | page | tuple | transaction | pid | mode | granted
      ---------------+----------+------+-------+-------------+-------+-----------------+---------
       artikl | relation | | | 5378326 | 31761 | AccessShareLock | t
      (1 rows)
      


      What am I doing wrong?