5.spring 整合 事务

一、spring整合 事务

  • 注解方式

    1. 添加Aop依赖

      <!-- spring aop支持 -->
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-aop</artifactId>
      	<version>${spring.version}</version>
      </dependency>
      <!-- aspectj支持 -->
      <dependency>
      	<groupId>org.aspectj</groupId>
      	<artifactId>aspectjweaver</artifactId>
      	<version>1.8.7</version>
      </dependency>
      
    2. 添加事务管理器

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
      				http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      				http://www.springframework.org/schema/context
      				http://www.springframework.org/schema/context/spring-context-4.3.xsd
      				http://www.springframework.org/schema/tx
          			http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      				http://www.springframework.org/schema/aop
      				http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
       
      	<!-- 定义事务管理器 -->
      	<bean id="transactionManager" 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="数据源名称" />
      	</bean>
      	<!--使用注释事务 -->
      	<tx:annotation-driven transaction-manager="transactionManager" />
      </beans>
      
    3. 添加@Transactional

  • 声明式事务

    1. 添加依赖

      <!-- spring aop支持 -->
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-aop</artifactId>
      	<version>${spring.version}</version>
      </dependency>
      <!-- aspectj支持 -->
      <dependency>
      	<groupId>org.aspectj</groupId>
      	<artifactId>aspectjweaver</artifactId>
      	<version>1.8.7</version>
      </dependency>
      
    2. 配置事务管理器

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
      				http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      				http://www.springframework.org/schema/context
      				http://www.springframework.org/schema/context/spring-context-4.3.xsd
      				http://www.springframework.org/schema/tx
          			http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      				http://www.springframework.org/schema/aop
      				http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
       
      	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
      	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="数据源名称" />
      	</bean>
      	<!--事务通知-->
      	<tx:advice id="txAdvice" transaction-manager="transactionManager">
      		<tx:attributes>
      			<tx:method name="方法名*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
      		 </tx:attributes>
      	</tx:advice>
      	<!-- 自动代理 -->
      	<aop:aspectj-autoproxy proxy-target-class="true" />
      
      	<!-- 事务处理 -->
      	<aop:config>
      		<!--切点-->
      		<aop:pointcut id="pc" expression="表达式" />
      		<!--切点通知-->
      		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
      	</aop:config>
      </beans>
      

二、事务常用属性

  • read-only

    指定事务是否为只读事务,默认值为 false;为了忽略那些不需要事务的方法,比如读取数据,可以设置 read-only 为 true。

  • propagation

    事务的传播行为,默认值为 REQUIRED。

    常量含义
    Propagation.REQUIRED如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。
    Propagation.REQUIRES_NEW创建一个新的事务,如果当前存在事务,则把当前事务挂起。
    Propagation.SUPPORTS如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。
    Propagation.NOT_SUPPORTED以非事务方式运行,如果当前存在事务,则把当前事务挂起。
    Propagation.NEVER以非事务方式运行,如果当前存在事务,则抛出异常。
    Propagation.MANDATORY如果当前存在事务,则加入该事务;如果当前没有事务,则抛出异常。
    Propagation.NESTED如果当前存在事务,则创建一个事务作为当前事务的嵌套事务来运行;如果当前没有事务,则该取值等价于Propagation.REQUIRED
  • rollbackFor

    用于指定能够触发事务回滚的异常类型,如果有多个异常类型需要指定,各类型之间可以通过逗号分隔。{xxx1.class, xxx2.class,……}t

    注意

    ​ @Transactional注解默认支持运行时异常。对于非运行时异常并不支持,所以需指定以上属性

  • noRollbackFor

    抛出 no-rollback-for 指定的异常类型,不回滚事务。{xxx1.class, xxx2.class,……}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默语玄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值