2 Replies Latest reply on Jun 17, 2011 3:15 AM by adamw

    Hibernate Envers and Spring JTA

    aminmc

      Hi

       

      I have a funny feeling this may have been asked before..so apologies.  I am struggling to get hibernate envers and spring jta to work together.  When running my application I can't see any audit entries when there is a successful transaction commit.  Been searching around and also looked at the JTA test that is in Hibernate Search and no luck.  I was wondering whether someone out there may have experienced this issue?  Here is sessionfactory xml snippet:

       

       

      <bean id="sessionFactory"

                class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

              <property name="dataSource">

                  <ref local="dataSource"/>

              </property>

              <property name="packagesToScan">

                  <list>

                      <value>org.aminmc.hibernate.envers.cascade</value>

                  </list>

              </property>

              <property name="hibernateProperties">

                  <props>

                      <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>

                      <prop key="hibernate.hbm2ddl.auto">create-drop</prop>

                      <prop key="show_sql">true</prop>

                      <prop key="format_sql">false</prop>

                      <prop key="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy

                      </prop>

                      <prop key="org.hibernate.envers.audit_strategy_validity_end_rev_field_name">REVEND</prop>

                      <prop key="org.hibernate.envers.revision_on_collection_change">false</prop>

                      <!-- 2 is TRANSACTION_READ_COMMITTED -->

                      <prop key="hibernate.connection.isolation">2</prop>

                      <!-- http://docs.jboss.org/hibernate/core/3.3/reference/en/html/architecture.html#architecture-current-session -->

                      <prop key="hibernate.current_session_context_class">org.hibernate.context.JTASessionContext</prop>

                      <prop key="hibernate.transaction.manager_lookup_class">

                          org.hibernate.transaction.BTMTransactionManagerLookup

                      </prop>

                      <prop key="hibernate.cache.use_second_level_cache">false</prop>

                      <prop key="hibernate.cache.use_query_cache">false</prop>

                      <prop key="hibernate.cache.region_prefix">integration</prop>

       

                  </props>

              </property>

              <property name="eventListeners">

                  <map>

                      <entry key="post-insert" value-ref="auditEventListener"/>

                      <entry key="post-update" value-ref="auditEventListener"/>

                      <entry key="post-delete" value-ref="auditEventListener"/>

                      <entry key="pre-collection-update" value-ref="auditEventListener"/>

                      <entry key="pre-collection-remove" value-ref="auditEventListener"/>

                      <entry key="post-collection-recreate" value-ref="auditEventListener"/>

                  </map>

              </property>

          </bean>

          <bean id="auditEventListener" class="org.hibernate.envers.event.AuditEventListener"/>

       

          

      <!-- From http://docs.codehaus.org/display/BTM/Spring+Framework13 -->

          <!--  Bitronix Transaction Manager embedded configuration -->

          <bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices">

              <!--  property serverId should not be set when BTMTransactionManagerLookup is configured as the

                      hibernate transaction manager lookup class -->

              <!-- <property name="serverId" value="spring-btm" /> -->

              <property name="journal" value="null"/>

          </bean>

       

       

          <!-- create BTM transaction manager -->

          <bean id="BitronixTransactionManager" factory-method="getTransactionManager"

                class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig,dataSource" destroy-method="shutdown"/>

       

       

          <!-- Spring JtaTransactionManager -->

          <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">

              <property name="transactionManager" ref="BitronixTransactionManager"/>

              <property name="userTransaction" ref="BitronixTransactionManager"/>

          </bean>

       

       

       

      Here is the code i call:

       

      transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                  @Override

                  protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {

                      final ClassA classA = new ClassA();

                      classA.setObjectId(UUID.randomUUID().toString());

                      classA.add(classB);

                      hibernateTemplate.save(classB);

       

       

                  }

              });

       

       

      I've been battling with this for a few days now and any help would be appreciated! Am I missing something?  I've tried various approaches and can't seem to see what i'm doing wrong...

       

      Cheers!

        • 1. Re: Hibernate Envers and Spring JTA
          aminmc

          An update to this is that if i use JPA it works fine, using SessionFactory and vanilla hibernate with JTA doesn't seem to work.

           

          I can provide a testcase if needed.

          • 2. Re: Hibernate Envers and Spring JTA
            adamw

            So the hibernateTemplate opens a new session, persists the entity and closes the session? Or what are the scopes of the transaction and the session?

             

            Also, you could take a look at AuditProcess, if the work units get added and then executed (doBeforeTransactionCompletion).

             

            Adam