spring事务管理

来源:互联网 发布:券老大淘宝优惠券 编辑:程序博客网 时间:2024/06/06 09:59

1、使用tx管理事务

    <aop:config>
         <aop:pointcut expression="execution(* com.lq.service.impl.UserServiceImpl.*(..))" id="pc"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" rollback-for="Exception" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>


2、使用拦截器管理事务

    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager" />
        <!-- 配置事务属性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="validate">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="declare*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="*">readOnly</prop>
            </props>
        </property>
    </bean>
    <!-- 使用动态代理方式注入事务管理拦截器 -->
    <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*ServiceImpl</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

1 0
原创粉丝点击