请教个问题,在ssh开发中,涉及到“事务管理”的问题,大家觉得事务管理应该在“业务层”还是“dao层”
我个人理解,应该放在“业务层”,因为"dao层"往往是原子操作了,因此不需要在这一层加事务管理。请大家讨论下哦

解决方案 »

  1.   

    DAO层未必都是原子的。但是我一般是事务配置在业务层。
      

  2.   

    我在以前的开发中,经常遇到过这样的问题,就是业务层方法和dao层方法同名,或者基本同名
    你看下这样的配置
     <props>
        <prop key="log*">PROPAGATION_REQUIRED</prop>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="*">PROPAGATION_REQUIRED</prop>
       </props>
    如果这样的话,那岂不是业务层和dao层都进行了事务管理了吗?
    不知道我理解可对?
      

  3.   

    你们是这样配置的?<!-- 事务管理器 -->
        <bean id="transactionManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
        <ref local="sessionFactory"/>
        </property>
        </bean>
        <!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true"/>
        </tx:attributes>
        </tx:advice>
        <!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
        类中所有方法需要,还需要参考tx:advice的设置 -->
        <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution(*
        com.sy.crm.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
        </aop:config>
        
      

  4.   

    <bean id="aaaImpl" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager" ref="transactionManager"/>
      <property name="target" ref="aaaImplTarget"/>
      <property name="transactionAttributes">
        <props>       
          <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
          <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
          <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
      </property>
    </bean>
    <bean id="aaaImplTarget" class="aaa.AAAImpl" >
    </bean>
      

  5.   

    业务层,感觉必须的.DAO层多麻烦,到业务层可能还得再管一次.
      

  6.   

    对,你说的对,不要在dao层处理事物,如果在dao层遇到什么异常啊,需要回滚呀等操作的时候,你要往上抛,抛带manager层去处理。(这可是专家级的做法o(∩_∩)o...哈哈)