Announcement Announcement Module
Collapse
No announcement yet.
Starting tx around service methods, not Dao methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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" />

    Comment


    • #3
      Alternative is to use programmatical transaction management via TransactionTemplate.

      Comment


      • #4
        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.

        Comment

        Working...
        X