6 Replies Latest reply on Jan 26, 2011 11:02 AM by galder.zamarreno

    Using infinispan as 2LC with hibernate

    amitmaniar

      Hi All,

       

      From last 2 weeks I am trying to use infinispan as 2LC with hibernate but did not succeed.

       

      I tried all given configuration. Initially Following is my requirements and now hoping some help from someone here.

      => I want to create cluster having >=2 nodes using Jboss AS.

      =>Single Database using postgresSQL 8.4.

      =>Hibernate as ORM

      =>Web application will just consist of JSP and servlets. (No EJB but some threads and schedulers will be there)

       

      While trying to configure Jboss for above requirements I came to know following things.

      =>I will have to enable 2LC with hibernate to share session factory

      =>To enable 2LC I will need some cache mechanism (Jboss cache or infinispan).

      =>I will have to use BMT (JTA) for concurrency.

       

      After configuring parameters for these requirements I am able to modify data from application. But my problem is that I am not able to maintain concurrency in transaction.

       

      May be I am missing some configuration.

       

      Test Case:

      I have 2 nodes of cluster on same machine but different serverPeerId and different set of ports.

      I have created one servlet which takes waitTime as request parameter and waits before commit.

      So I am calling this servlet from 1st node with 20sec wait time and calling same servlet from 2nd node with no wait time.

       

      Result:

      By the wait time of 1st node 2nd updates record in DB and 1st should be rollbacked as version of data has been changes as per MVCC but no exception or warning or rollback of transaction....

       

      hibernate.cfg.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      <hibernate-configuration>
          <session-factory name="SessionFactory">
              <property name="connection.datasource">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>

       

      Connection to PostgreSQL is managed using xa-datasource

      postgres-xa-ds.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <datasources>
          <xa-datasource>
              <track-connection-by-tx>true</track-connection-by-tx>
              <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                      <xa-datasource-property name="ServerName">192.168.1.1</xa-datasource-property>
                      <xa-datasource-property name="PortNumber">5432</xa-datasource-property>
                      <xa-datasource-property name="DatabaseName">testdb</xa-datasource-property>
                      <xa-datasource-property name="User">user</xa-datasource-property>
                      <xa-datasource-property name="Password">pwd</xa-datasource-property>

       

                      <jndi-name>PostgresDS</jndi-name>
                      <use-java-context>false</use-java-context>
                      <transaction-isolation>TRANSACTION_REPEATABLE_READ</transaction-isolation>

       

                      <max-pool-size>20</max-pool-size>
                      <min-pool-size>2</min-pool-size>

       

                      <check-valid-connection-sql>select 1</check-valid-connection-sql>

       

                      <metadata><type-mapping>PostgreSQL</type-mapping></metadata>
              </xa-datasource>
      </datasources>

       

      infi-min.xml

      <?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">

       

      <global>
         
          <globalJmxStatistics enabled="true" jmxDomain="infinispan" allowDuplicateDomains="true" />
              <transport distributedSyncTimeout="60000" transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport">
                  <properties>
                      <property name="configurationFile" value="jgroups-tcp.xml" />
                  </properties>
              </transport>

      </global>

       

      <default>
          <transaction
              transactionManagerLookupClass="org.infinispan.transaction.lookup.GenericTransactionManagerLookup"
              syncRollbackPhase="true"
              syncCommitPhase="true"
              useEagerLocking="true" />
         
          <clustering mode="replication">
              <stateRetrieval timeout="20000" fetchInMemoryState="true"/>
              <sync replTimeout="20000"/>
          </clustering>

       

          <locking
              isolationLevel="REPEATABLE_READ"
              lockAcquisitionTimeout="20000"
              writeSkewCheck="true"
              concurrencyLevel="500"
              useLockStriping="false" />

       

      </default>
      </infinispan>

      contact.hbm.xml

      <?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="bean.Contact" table="CONTACT">
              <cache usage="transactional" />
              <id name="id" type="long" column="ID">
                  <generator class="sequence">
                      <param name="sequence">contact_id</param>
                  </generator>
              </id>
              <property name="firstName">
                  <column name="FIRSTNAME" />
              </property>
              <property name="lastName" lazy="true">
                  <column name="LASTNAME"/>
              </property>
              <property name="email">
                  <column name="EMAIL"/>
              </property>
          </class>
      </hibernate-mapping>

       

      Can anybody help me???