Spring事务控制

一、学习目标

  1. 掌握spring的注解式事务控制
  2. 了解spring的配置文件事务控制
  3. 了解spring的事务传播行为

二、配置spring的事务管理操作

2.1 spring管理事务的原理(了解)

❤️严正声明 该节无需配置和练习

❤️严正声明 该节无需配置和练习

❤️严正声明 该节无需配置和练习

❤️严正声明 该节无需配置和练习

❤️严正声明 该节无需配置和练习

2.1.1 另一种配置aop编程的方式
2.1.1.1 实现MethodInterceptor接口
package com.itheima.adivce;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LoggerAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕通知前");
        Object o = methodInvocation.proceed();
        System.out.println("环绕后");
        return o;
    }
}

2.1.1.2 aop配置
<bean id="loggerAdvice" class="com.itheima.adivce.LoggerAdvice"></bean>

<aop:config>
    <aop:pointcut id="pt" expression="execution(* com.itheima.service..*.*(..))"></aop:pointcut>
    <!--配置通知类-->
    <aop:advisor advice-ref="loggerAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
2.1.2 spring内置的事务管理对象原始配置方式

前提导入maven依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>

如同上面的原理 spring中内置了 事务管理的MethodInterceptor

在这里插入图片描述

所以我们也可以采用最原始的配置

  <!--
    这是人家定义事务管理器
  -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!--
    事务管理属性
    -->
  <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    <property name="nameMap">
      <map>
        <!--findById方法只读事务-->
        <entry key="findById" >
          <bean class="org.springframework.transaction.interceptor.DefaultTransactionAttribute">
            <property name="readOnly" value="true"></property>
          </bean>
        </entry>
        <!--其他方法 正常事务控制 -->
        <entry key="*" >
          <bean class="org.springframework.transaction.interceptor.DefaultTransactionAttribute"></bean>
        </entry>
      </map>
    </property>
  </bean>

  <!--spring写了一个事务环绕通知 他也许需要一个事务管理器对象-->
  <bean id="transactionAdvice" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" ></property>
    <!--事务管理属性-->
    <property name="transactionAttributeSource" ref="transactionAttributeSource"></property>
  </bean>
  <aop:config>
    <aop:pointcut id="pt" expression="execution(* com.itheima.service..*.*(..))"></aop:pointcut>

    <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pt"></aop:advisor>

  </aop:config>

可见spring的事务管理底层也是采用了aop编程加上transactionManager方式来实现的

2.2 spring的xml方式配置事务管理

上面代码虽然可以做事务控制,但是配置太麻烦了 所以spring 提供了更加方便的配置方案

  <!--
    这是人家定义事务管理器
  -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>


  <!--简化配置了-->
  <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="find*" read-only="true" />
      <!--
        isolation:事务的隔离级别
        timeout:超时时间 -1
        rollback-for:那些异常才会回滚
       no-rollback-for:那些异常不会滚
       propagation: 事务传播行为

      -->
      <!--<tx:method name="*" read-only="false" isolation="DEFAULT" timeout="-1" rollback-for="java.lang.Throwable" no-rollback-for="java.lang.ArithmeticException"/>-->
      <tx:method name="*" read-only="false" />
    </tx:attributes>
  </tx:advice>



  <aop:config>
    <aop:pointcut id="pt" expression="execution(* com.itheima.service..*.*(..))"></aop:pointcut>

    <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pt"></aop:advisor>

  </aop:config>

简化了配置 通过tx命名空间的解析注入到spring容器对象中TransactionInterceptor

源码体现(无需理解!!!)

在这里插入图片描述

2.3 spring的注解方式配置事务管理(必须掌握!!!)

虽然xml注解已经很简化 但是还没有注解使用方便 接下来我们使用注解完成事务控制

2.3.1 配置事务注解驱动
<!--
    这是人家定义事务管理器
  -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 配置了 事务管理注解驱动-->
  <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
2.3.2 在service类上配置事务注解
package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;


@Service
//把这个注解加在类上 这个类的所有方法 自动加上事务管理了
//方法上可以覆盖类上的注解
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Override
    @Transactional(readOnly = true)
    public Account findById(int id) {
        return accountDao.findById(id);
}

    @Override
    @Transactional
    public void update(Account account) {
        accountDao.update(account);
    }

    @Override
    @Transactional
    public void del(int id) {
        accountDao.del(id);
    }

    @Override
    @Transactional
    public void transfer(int from, int to, double money) {
        accountDao.updateMoney(from,-money);
        int i=5/0;
        accountDao.updateMoney(to,money);
    }
}

2.3.3 @Transactional注解属性解析

/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;
import org.springframework.transaction.TransactionDefinition;

/**
 * Describes a transaction attribute on an individual method or on a class.
 *
 * <p>At the class level, this annotation applies as a default to all methods of
 * the declaring class and its subclasses. Note that it does not apply to ancestor
 * classes up the class hierarchy; methods need to be locally redeclared in order
 * to participate in a subclass-level annotation.
 *
 * <p>This annotation type is generally directly comparable to Spring's
 * {@link org.springframework.transaction.interceptor.RuleBasedTransactionAttribute}
 * class, and in fact {@link AnnotationTransactionAttributeSource} will directly
 * convert the data to the latter class, so that Spring's transaction support code
 * does not have to know about annotations. If no rules are relevant to the exception,
 * it will be treated like
 * {@link org.springframework.transaction.interceptor.DefaultTransactionAttribute}
 * (rolling back on {@link RuntimeException} and {@link Error} but not on checked
 * exceptions).
 *
 * <p>For specific information about the semantics of this annotation's attributes,
 * consult the {@link org.springframework.transaction.TransactionDefinition} and
 * {@link org.springframework.transaction.interceptor.TransactionAttribute} javadocs.
 *
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 1.2
 * @see org.springframework.transaction.interceptor.TransactionAttribute
 * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute
 * @see org.springframework.transaction.interceptor.RuleBasedTransactionAttribute
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {

	@AliasFor("transactionManager")
	String value() default "";


	@AliasFor("value")
	String transactionManager() default "";
	//事务的传播行为配置 有七种之多 默认挺好
	Propagation propagation() default Propagation.REQUIRED;
	//事务的隔离级别 默认挺好
	Isolation isolation() default Isolation.DEFAULT;

	//超时时间 默认挺好
	int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;

	//是否是只读事务
	boolean readOnly() default false;
	//那些类型的回滚
	Class<? extends Throwable>[] rollbackFor() default {};

	String[] rollbackForClassName() default {};

	//那些类型不回滚
	Class<? extends Throwable>[] noRollbackFor() default {};

	String[] noRollbackForClassName() default {};
}

三、spring的事务传播行为

事务的传播行为:

​ 指的是如果多个service互相调用的时候 如果都是事务控制 到底怎么协商处理的方式

spring支持其中七种事务传播行为:

REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常.
REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。
NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起. 真的破罐破摔
NEVER:以非事务方式运行,如果当前存在事务,抛出异常. 
NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作。

具体的区别和原理 请参考视频和参考示例工程

❤️请老铁们保留该视频和示例工程 为以后找工作面试的时候 复习 这是一个面试题

❤️请老铁们保留该视频和示例工程 为以后找工作面试的时候 复习 这是一个面试题

❤️请老铁们保留该视频和示例工程 为以后找工作面试的时候 复习 这是一个面试题

❤️请老铁们保留该视频和示例工程 为以后找工作面试的时候 复习 这是一个面试题

❤️请老铁们保留该视频和示例工程 为以后找工作面试的时候 复习 这是一个面试题

四、总结

今天主要目标 掌握注解的事务控制

步骤一 导入maven依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>

步骤二 配置事务管理器和注解驱动

<!--
    这是人家定义事务管理器
  -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置了 事务管理注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

步骤三 配置注解

package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;


@Service
//把这个注解加在类上 这个类的所有方法 自动加上事务管理了
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Override
    @Transactional(readOnly = true)
    public Account findById(int id) {
        return accountDao.findById(id);
}

    @Override
    public void update(Account account) {
        accountDao.update(account);
    }

    @Override
    public void del(int id) {
        accountDao.del(id);
    }

    @Override
    public void transfer(int from, int to, double money) {
        accountDao.updateMoney(from,-money);
        int i=5/0;
        accountDao.updateMoney(to,money);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

娃娃 哈哈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值