2 Replies Latest reply on Sep 7, 2010 7:46 AM by Manik Surtani

    Repetable Read does not throw exception already updated touple

    Siddharth Shah Newbie

      Hello,

        • 1. Re: Repetable Read does not throw exception already updated touple
          Siddharth Shah Newbie

          Parallel transaction updates record, which is not desirable behavoiur for my application, need to fail subsequent transation on same records

           

          Here I am reading Same record from two different session objects

           

          Time  Read  Write

             1      Tx1             (Read database Record,will fetch from database)

             2      Tx2             (Read same database record, will be served from 2LC (Infinispan), New instance object as different session is used)

             3                Tx1   (Database update successful)

             4                Tx2   (Again database update successful , This should not happen in case of repeatable read as before writing transaction must have track the fetch version and intermediate commit has been done on time 3 is done, so one this event exception must arise)

           

           

          Code Snippet

               try{
                      session = f.sessionFactory.openSession();
                      Transaction t = session.beginTransaction();
                      Contact con = (Contact)session.load(Contact.class, new Long(4));
                      System.out.println("Session last name : "+con.getLastName());
                      con.setLastName(request.getParameter("a"));
                     
                      session1 = f.sessionFactory.openSession();
                      Transaction t1 = session1.beginTransaction();
                      Contact con1 = (Contact)session1.load(Contact.class, new Long(4));
                      System.out.println("Session1 last name : "+con1.getLastName());
                      con1.setLastName(request.getParameter("b"));
                     
                      if(con == con1){
                          System.out.println("objects are same");
                      }else{
                          System.out.println("objects are diffent");
                      }

           

                      session.save(con);
                      t.commit();
                     
                      try{
                          Thread.sleep(1000);
                      }catch (InterruptedException e) {
                      }
                      session1.save(con1);
                      t1.commit();
                  }catch (Exception e) {
                      e.printStackTrace();
                  }
                  finally{
                      session.close();
                      session1.close();
                  }

           

           

           

          Hibernate Configuration

           

          <hibernate-configuration>
                  <session-factory>
                          <property name="connection.datasource">java:PostgresDS</property>
                          <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
                         
                          <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
                          <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
                          <property name="hibernate.current_session_context_class">jta</property>
                         
                          <property name="jta.UserTransaction">java:comp/UserTransaction</property>
                         
                          <property name="hibernate.cache.use_second_level_cache">true</property>
                          <property name="hibernate.cache.use_query_cache">false</property>
                          <property name="hibernate.show_sql">true</property>
                         
                          <property name="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</property>
                          <property name="hibernate.cache.infinispan.statistics">true</property>
                          <property name="hibernate.cache.infinispan.cfg">infi-min.xml</property>
                         
                          <property name="hibernate.cache.infinispan.entity.eviction.strategy">LRU</property>
                          <property name="hibernate.cache.infinispan.entity.eviction.wake_up_interval">10000</property>
                          <property name="hibernate.cache.infinispan.entity.eviction.max_entries">5000</property>
                          <property name="hibernate.cache.infinispan.entity.expiration.lifespan">60000</property>
                          <property name="hibernate.cache.infinispan.entity.expiration.max_idle">30000</property>

           

                          <mapping resource="contact.hbm.xml" />
                      </session-factory>
          </hibernate-configuration>

           

           

          for mapping class to DB cache is transaction configured

           

          Infinispan Configuration

           

           

          <?xml version="1.0" encoding="UTF-8"?>
          <infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="urn:infinispan:config:4.0 http://www.infinispan.org/schemas/infinispan-config-4.0.xsd"
                xmlns="urn:infinispan:config:4.0">

           

             <!-- *************************** -->
             <!-- System-wide global settings -->
             <!-- *************************** -->

           

              <global>
                  <globalJmxStatistics enabled="true" jmxDomain="infinispan" allowDuplicateDomains="true" />
                 
              </global>
              <default>
                <locking
                      isolationLevel="REPEATABLE_READ"
                      lockAcquisitionTimeout="20000"
                      writeSkewCheck="true"
                      concurrencyLevel="500"
                      useLockStriping="true"
                      />
              </default>
          </infinispan>

           

           

           

           

          Logs

          19:58:40,285 INFO  [org.hibernate.cfg.Configuration] configuring from resource: /hibernate.cfg.xml
          19:58:40,301 INFO  [org.hibernate.cfg.Configuration] Configuration resource: /hibernate.cfg.xml
          19:58:40,332 INFO  [org.hibernate.cfg.Configuration] Reading mappings from resource : contact.hbm.xml
          19:58:40,379 INFO  [org.hibernate.cfg.HbmBinder] Mapping class: bean.Contact -> CONTACT
          19:58:40,379 INFO  [org.hibernate.cfg.Configuration] Configured SessionFactory: null
          19:58:40,379 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
          19:58:40,379 INFO  [org.hibernate.connection.DatasourceConnectionProvider] Using datasource: java:PostgresDS
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] RDBMS: PostgreSQL, version: 8.4.3
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.4 JDBC3 (build 701)
          19:58:40,379 INFO  [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
          19:58:40,379 INFO  [org.hibernate.engine.jdbc.JdbcSupportLoader] Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
          19:58:40,379 INFO  [org.hibernate.transaction.TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
          19:58:40,379 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
          19:58:40,379 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
          19:58:40,379 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Automatic flush during beforeCompletion(): disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Automatic session close at end of transaction: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] JDBC batch size: 15
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] JDBC batch updates for versioned data: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Scrollable result sets: enabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] JDBC3 getGeneratedKeys(): enabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Connection release mode: auto
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Default batch fetch size: 1
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Generate SQL with comments: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Order SQL updates by primary key: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Order SQL inserts for batching: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
          19:58:40,379 INFO  [org.hibernate.hql.ast.ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Query language substitutions: {}
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] JPA-QL strict compliance: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Second-level cache: enabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Query cache: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Cache region factory : org.hibernate.cache.infinispan.InfinispanRegionFactory
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Optimize cache for minimal puts: enabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Structured second-level cache entries: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Echoing all SQL to stdout
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Statistics: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Deleted entity synthetic identifier rollback: disabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Default entity-mode: pojo
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Named query checking : enabled
          19:58:40,379 INFO  [org.hibernate.cfg.SettingsFactory] Check Nullability in Core (should be disabled when Bean Validation is on): enabled
          19:58:40,394 INFO  [org.hibernate.impl.SessionFactoryImpl] building session factory
          19:58:40,394 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
          19:58:40,753 INFO  [org.infinispan.factories.GlobalComponentRegistry] Infinispan version: Infinispan 'Radegast' 4.1.0.CR3
          19:58:40,862 INFO  [org.infinispan.jmx.ComponentsJmxRegistration] Could not register object with name: infinispan19:cache-name=bean.Contact(local),jmx-resource=Cache
          19:58:40,862 INFO  [org.infinispan.jmx.CacheJmxRegistration] MBeans were successfully registered to the platform mbean server.
          19:58:40,862 INFO  [org.infinispan.factories.ComponentRegistry] Infinispan version: Infinispan 'Radegast' 4.1.0.CR3
          19:58:40,878 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
          19:58:40,878 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
          19:58:40,878 INFO  [STDOUT] Hibernate: select contact0_.ID as ID2_0_, contact0_.FIRSTNAME as FIRSTNAME2_0_, contact0_.LASTNAME as LASTNAME2_0_, contact0_.EMAIL as EMAIL2_0_ from CONTACT contact0_ where contact0_.ID=?

          19:58:40,893 INFO  [STDOUT] Session last name : 89

          19:58:40,893 INFO  [STDOUT] objects are diffent

          19:58:40,893 INFO  [STDOUT] Hibernate: update CONTACT set FIRSTNAME=?, LASTNAME=?, EMAIL=? where ID=?

          19:58:40,940 INFO  [STDOUT] Session1 last name : 89