(14)spring_ssh整合_声明式事务

<!--事物管理对象 -->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
<!--配置事物的通知  -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- 事物的属性,其实就是为切面中的哪些方法进行事物的配置 -->
        <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--aop配置信息  -->
    <aop:config>
        <aop:pointcut id="pcut" expression="execution(* spring.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pcut"/>
    </aop:config>

1.如果删除组需要先删除组内的用户,当删除组发生异常,此时已经删除了组内的用户了,如果事务在dao层的话,执行完每一个dao就提交了事务,此时已经把删除用户的事务提交了,就rollback不回来了,所以事务应该在service层面

2.service层通常进行逻辑处理等,如果某一步出错,就应该rollback,所以在service层进行事务处理较为合理,expression="execution(* spring.dao.*.*(..))"改为expression="execution(* spring.service.*.*(..))"即可实现事务从dao层到service层。

猜你喜欢

转载自blog.csdn.net/HZPHYT/article/details/84654233