Spring--Spring5事物管理(2)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: Spring--Spring5事物管理(2)

事务操作(声明式事务管理参数配置)


@Transactional相关参数


image.png


propagation:事务传播行为


(1)事物方法?


事物方法就是对数据库表数据进行变化的操作,如增加删除数据的方法就是事物方法


(2)事物传播行为?


指的就是当一个事物方法被另一个事务方法调用时,这个事务改如何进行

例如当A事物方法调用B事物方法时,B是继续在A事物方法中运行呢,还是为自己新开一个事物运行呢,这就是B事物方法传播行为决定的~

这里视频讲的一笔带过了,想了解详细的话要去补一下mysql基础!

视频这里主要是讲这些在spring中怎么配置

image.png

Spring框架事务传播行为有7种

主要了解这两种

REQUIRED

如果add方法本身有事务,调用update方法之后,update使用当前add方法里面事务

如果add方法本身没有事务,调用update方法之后,创建新事务

REQUIRED_NEW

使用add方法调用updato方法,如果add无论是否有事务,都创建新的事务

image.png


timeout:超时时间


(1)事务需要在一定时间内进行提交,如果不提交进行回滚

(2)默认值是-1,设置时间以秒单位进行计算


readOnly:是否只读


(1)读:查询操作,写:添加修改删除操作

(2) readOnly 默认值false,表示可以查询,可以添加修改删除操作

(3)设置readOnly值是true,设置成true之后,只能查询


rollbackFor:回滚


(1)设置出现哪些异常进行事务回滚


noRollbackFor:不回滚


(1)设置出现哪些异常不进行事务回滚


事物操作(XML声明式事务管理)


<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd                          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd                          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--    组件扫描-->    <context:component-scan base-package="com.caq"></context:component-scan>    <!-- 数据库连接池 -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"          destroy-method="close">        <property name="url" value="jdbc:mysql:///user_db" />        <property name="username" value="root" />        <property name="password" value="root" />        <property name="driverClassName" value="com.mysql.jdbc.Driver" />    </bean><!--    JdbcTemplate对象-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!--        注入dataSource-->        <property name="dataSource" ref="dataSource"></property>    </bean><!--    1.创建事务管理器-->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--        注入数据源-->        <property name="dataSource" ref="dataSource"></property>    </bean><!--    2.配置通知-->    <tx:advice id="txadvice">        <tx:attributes><!--            指定那种规则的方法上面添加事物-->            <tx:method name="accountMoney" propagation="REQUIRED"/><!--            <tx:method name="account*"/>-->        </tx:attributes>    </tx:advice><!--    3.配置切入点和切面-->    <aop:config><!--        给UserService类里的所有方法都运行这个规则-->        <aop:pointcut id="pt" expression="execution(* com.caq.spring5.service.UserService.*(..)"/><!--    配置切面-->        <aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>    </aop:config></beans>
@Autowiredprivate UserDao userDao;public void accountMoney(){    userDao.reduceMoney();    int i = 10/0;    userDao.addMoney();}@Test    public void test2(){        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");        UserService userService = context.getBean("userService", UserService.class);        userService.accountMoney();    }
事物会发生回滚,数据库表数据不变
事务操作(完全注解声明式事务管理)


注解类


package com.caq.spring5.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;@Configuration //配置类@ComponentScan(basePackages = "com.caq")    //扫描com.caq包下所有@EnableTransactionManagement    //开启事物public class TxConfig {    //创建数据库连接池    @Bean    public DruidDataSource getDruidDataSource() {        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName("com.mysql.jdbc.Driver");        dataSource.setUrl("jdbc:mysql:///user_db");        dataSource.setUsername("root");        dataSource.setPassword("root");        return dataSource;    }    //    JdbcTemplate对象    @Bean    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {//        到ioc容器中根据数据类型找到dataSource        JdbcTemplate jdbcTemplate = new JdbcTemplate();        //        注入dataSource        jdbcTemplate.setDataSource(dataSource);        return jdbcTemplate;    }    //声明事物管理器    @Bean    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();        transactionManager.setDataSource(dataSource);        return transactionManager;    }}


测试


@Test
public void test3(){
    ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    userService.accountMoney();
}

事物会发生回滚,数据库表数据不变



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
19天前
|
数据采集 Java 程序员
【Spring】Spring AOP
【Spring】Spring AOP
|
19天前
|
XML Java Maven
【Spring】——Spring的创建与使用(一)
【Spring】——Spring的创建与使用
36 0
【Spring】——Spring的创建与使用(一)
|
19天前
|
存储 Java Maven
【Spring】——Spring的创建与使用(二)
【Spring】——Spring的创建与使用
54 0
【Spring】——Spring的创建与使用(二)
|
10月前
|
Java Spring 容器
【Spring】Spring常用注解(中)
【Spring】Spring常用注解(中)
33 0
|
10月前
|
XML Java 数据格式
【Spring】Spring常用注解(上)
【Spring】Spring常用注解(上)
38 0
|
10月前
|
Java Spring 容器
【Spring】Spring常用注解(下)
【Spring】Spring常用注解(下)
56 0
|
XML Java API
Spring--Spring5事物管理(1)
Spring--Spring5事物管理(1)
48 0
|
SQL XML Java
Spring--Spring5总结
Spring--Spring5总结
59 0
|
XML Java 数据格式
Spring基础篇:使用注解方式实现Spring提供的AOP
使用注解方式实现Spring提供的AOP
76 0
|
存储 XML Java
Spring【Spring的创建与使用】
Spring【Spring的创建与使用】
Spring【Spring的创建与使用】