Spring-AOP基于xml的事务控制

Spring-AOP基于xml的事务控制

修改xml模板中限定

添加aop 和 事务管理器tx限定

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


配置事务管理器

 <bean id="transManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

配置事务通知

 <tx:advice id="txAdvice" transaction-manager="transManager"></tx:advice>

配置切入点aspect并建立切入点表达式和通知的对应关系

 <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.serviceImpl.UserServiceImpl.*(..))"></aop:advisor>
    </aop:config>

tx:advice/ 标签中置事务管理器属性

 <!-- 配置事务属性:
        isolation:事务隔离级别,默认值DEFAULT,表示使用数据库的隔离级别
        no-rollback-for:指定某个异常发生时,不进行回滚;发生其它异常时,进行回滚;不指定默认值,表示任何异常都回滚
        propagation:事务的传播行为,默认值REQUIRED表示一定有事务,一般用于增删改,查询方法可使用SUPPORTS
        read-only:指定事务是否只读,只有查询方法才默认true,其它默认false
        rollback-for:指定某个异常发生时,进行回滚,其它异常发生时,不进行回滚;不指定默认值,任何异常都回滚
        timeout
        -->
        <tx:attributes>
            <tx:method name="*"
                       isolation="DEFAULT"
                       no-rollback-for=""
                       propagation="REQUIRED"
                       read-only="false"
                       rollback-for=""
                       timeout="10"/>
        </tx:attributes>

事务管理器属性说明

  • isolation:事务隔离级别,默认值DEFAULT,表示使用数据库的隔离级别
  • no-rollback-for:指定某个异常发生时,不进行回滚;发生其它异常时,进行回滚;不指定默认值,表示任何异常都回滚
  • propagation:事务的传播行为,默认值REQUIRED表示一定有事务,一般用于增删改,查询方法可使用SUPPORTS
  • read-only:指定事务是否只读,只有查询方法才默认true,其它默认false
  • rollback-for:指定某个异常发生时,进行回滚,其它异常发生时,不进行回滚;不指定默认值,任何异常都回滚
  • timeout:超时限制,默认值-1,表示永不超时;如果不设置默认值,就默认-1,没有超时限制;如果设置,则以秒为单位
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值