Announcement Announcement Module
Collapse
No announcement yet.
Unique transaction in Spring for a service Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unique transaction in Spring for a service

    Hi,

    I used the search form to find an answer but i don't have the correct word keys ...

    I am on Spring 2.5 and Hibernate 2.

    My *DAOImpl classes uses HibernateTemplate to execute database actions.

    I use org.springframework.orm.hibernate.HibernateTransac tionManager to specify a transaction strategy for my services. Each service execute several DAO actions (read and write).
    Strategy is basic : open/close transaction on method calls + rollback on a specific exception.

    My problem : each DAO method executes a commit in database but I want a unique transaction for a single service call ...

    How can I make the DAO methods not calling several commits whithin a single service call ?

    Thx

  • #2
    Code:
    <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource"/></property>
            <property name="mappingResources">
                <list>
                    <value>MyRecord.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.autocommit">false</prop>			
            </props>
            </property>
        </bean>

    Code:
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        	<property name="dataSource"><ref local="dataSource"/></property>
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
            <property name="globalRollbackOnParticipationFailure" value="false" />
        </bean>
    
    <tx:advice id="tx-advice" transaction-manager="transactionManager">
    	  <tx:attributes>
    	    <tx:method name="perform" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
    	    <tx:method name="updateRecords" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
    	    <tx:method name="createRecords" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
    	  </tx:attributes>	
    	</tx:advice>
    Any Idea ?

    Comment


    • #3
      Where did you set your transaction?
      On Service or on DAO?

      each DAO method executes a commit in database, ...
      You set the commit or it's added automatically?

      Comment


      • #4
        thx for replying,

        I "explicitly" set my transaction on service methods this way :
        Code:
        <tx:advice id="tx-advice" transaction-manager="transactionManager">
        	  <tx:attributes>
        	    <tx:method name="perform" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
        	    <tx:method name="updateRecords" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
        	    <tx:method name="createRecords" propagation="REQUIRED" isolation="DEFAULT" rollback-for="SystemException"/>
        	  </tx:attributes>	
        	</tx:advice>
        But, in logs, I can see that Spring component open/close a transaction whithin the DAO method (with flushing).

        Code:
        DEBUG - UpdateLineService.updateRecords(25) | BEGIN UPDATE ------------------>
        DEBUG - MyRecordDAOHibernateWithSpring.saveRecord(27) | DAO begin
        DEBUG - SessionFactoryUtils.doGetSession(318) | Opening Hibernate Session
        DEBUG - HibernateAccessor.flushIfNecessary(389) | Eagerly flushing Hibernate session
        DEBUG - SessionFactoryUtils.closeSession(789) | Closing Hibernate Session
        DEBUG - MyRecordDAOHibernateWithSpring.saveRecord(29) | DAO end
        I don't see how to loop on dao calls (MyRecordDAOHibernateWithSpring.saveRecord) in a unique service method (UpdateLineService.updateRecords) with a unique commit ...

        Comment


        • #5
          Only specifing a tx:advice is useless... You also need to tell WHERE/WHEN to apply the advice (I suggest a read of the tx chapter of the reference guide)

          Comment


          • #6
            Thx

            I added the AOP code in spring conf. but I had errors because I used incorrect versions of cglib, javassist and JTA (for testing purpose of course) ... Everything is working now.

            I am currently working on a liferay v4 and I changed a service to make it use a single transaction.

            thx

            Comment

            Working...
            X