Results 1 to 4 of 4

Thread: Starting tx around service methods, not Dao methods

  1. #1
    Join Date
    Jan 2006
    Location
    El Dorado
    Posts
    80

    Default Starting tx around service methods, not Dao methods

    Hey,
    I have this configuration:
    Code:
        <bean id="proDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="txManager"/>
            <property name="target" ref="proDaoTarget"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="remove*">PROPAGATION_REQUIRED</prop>
                    <prop key="load*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
        <bean id="proService" class="com.ProServiceImpl">
            <property name="proDao" ref="proDao"/>
        </bean>
    How to start transactions when ProService methods start.
    Consider this service method:
    Code:
    public void doSomething() {
      /*
       proDao load object1
       proDao load object2
       connect object1 to object2
       save changed to the database
      */
    }
    doSomething() statements should be grouped inside a transaction
    Any ideas?
    My environment:
    Spring 2.5
    Hibernate 3
    Thanks in advance.

  2. #2
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    Use service as target for proxy:

    Code:
        <bean id="proService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="txManager"/>
            <property name="target" ref="proServiceTarget"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="remove*">PROPAGATION_REQUIRED</prop>
                    <prop key="load*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean id="proServiceTarget" class="com.ProServiceImpl">
            <property name="proDao" ref="proDao"/>
        </bean>
    
        <bean id="proDao" class="ProDaoImpl" />

  3. #3
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    Alternative is to use programmatical transaction management via TransactionTemplate.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Well first off all the service layer should be the layer that is transactional NOT your dao layer. As Andrei already pointed out simply replace the target and interface. However I suggest to remove the TransactionalProxyFactoryBean and simply use an aop:config and tx:advice. This will greatly reduce the amount of configuration you have to do.

    Code:
    <bean id="proService" class="com.ProServiceImpl">
      <property name="proDao" ref="proDao"/>
    </bean>
    
    <aop:config>
      <aop:pointcut id="serviceMethod" expression="execution(* *..*ServiceImpl.*(..))" />
      <aop:advice pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>
    
    <tx:advice id="txAdvice">
      <tx:attributes>
        <tx:method name="save*" />
        <tx:method name="remove*" />
        <tx:method name="load*" readOnly="true"/>
        <tx:method name="delete*" />
      </tx:attributes>
    </tx:advice>
    Now automatically every service (following the naming convention that a class should end in ServiceImpl) is transactional without adding the overhead of a TransactionalProxyFactoryBean.
    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

Posting Permissions

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