Results 1 to 6 of 6

Thread: Unique transaction in Spring for a service

  1. #1
    Join Date
    May 2011
    Posts
    10

    Default 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. #2
    Join Date
    May 2011
    Posts
    10

    Default

    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 ?

  3. #3
    Join Date
    Apr 2011
    Posts
    107

    Default

    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?

  4. #4
    Join Date
    May 2011
    Posts
    10

    Default

    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 ...

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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)
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    May 2011
    Posts
    10

    Default 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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •