博客
关于我
spring控制事务:声明式事务(XML)事务的传播行为
阅读量:322 次
发布时间:2019-03-03

本文共 8172 字,大约阅读时间需要 27 分钟。

声明式事务(XML)

使用spring提供的专用于mybatis的事务管理器在xml中声明式事务

声明式事务需要使用到的标签

tx配置

进行<tx 标签的使用需要在xml头部导入命名空间

xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd

声明式事务需要用到的事务管理器类

< tx:advice> 标签

⚫ 名称:tx:advice

⚫ 类型:标签
⚫ 归属:beans标签
⚫ 作用:专用于声明事务通知
⚫ 格式:

⚫ 基本属性:

◆ id :用于配置aop时指定通知器的id
◆ transaction-manager :指定事务管理器bean

< tx:attributes> 标签

⚫ 名称:tx:attributes

⚫ 类型:标签
⚫ 归属:tx:advice标签
⚫ 作用:定义通知属性
⚫ 格式:

⚫ 基本属性:

◆ 无

< tx:method>标签

名称:tx:method

⚫ 类型:标签
⚫ 归属:tx:attribute标签
⚫ 作用:设置具体的事务属性
⚫ 格式:

⚫ 说明:

通常事务属性会配置多个,包含1个读写的全事务属性,1个只读的查询类事务属性

tx:method属性 争对方法配置不同方法的事务信息

模板

⚫ 使用tx命名空间配置事务专属通知类

⚫ 使用aop:advisor在AOP配置中引用事务专属通知类

转账案列进行事务管理(xml声明式事务)

mysql数据表

下面的案列就是对这两个数据进行更新的事务管理

在这里插入图片描述

搭建一个maven项目

在这里插入图片描述

pom

4.0.0
com.fs
day04_spring_AOP_Transaction_01
1.0-SNAPSHOT
org.springframework
spring-context
5.1.9.RELEASE
org.springframework
spring-test
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
org.mybatis
mybatis-spring
2.0.1
org.mybatis
mybatis
3.5.5
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.20
org.projectlombok
lombok
1.18.12
org.aspectj
aspectjweaver
1.9.5
junit
junit
4.12
test

配置文件(主配置文件中有详细的注释解释)

applicationContext.xml
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.93.132:3306/testjdbc.username=rootjdbc.password=root

pojo 实体类

Account
package com.fs.pojo;import lombok.Data;@Datapublic class Account {       private Integer id;    private String name;    private Double money;}

dao

AccountDao
package com.fs.dao;import com.fs.pojo.Account;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;public interface AccountDao {       @Select("select * from account")    List
findAll();//查询所有 //根据名字修改money @Update("UPDATE account SET money = #{money} WHERE name = #{name}") void transferMoney(Account account); //根据名字查询账户信息 @Select("select * from account where name = #{name}") Account findAccountByName(@Param("name") String name);}

service

AccountService
package com.fs.service;import com.fs.pojo.Account;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;public interface AccountService {       List
findAll(); Account findAccountByName(String name); void transferMoneyAtoB(String aName,String bName,Integer money);}

impl.AccountServiceImpl 代码中有spring事务管理器在业务层硬编码进行事务管理的代码(注释了)

在业务类中,我也使用编码的方式对spring的业务层进行了事务控制(注释代码就是)

package com.fs.service.impl;import com.fs.dao.AccountDao;import com.fs.pojo.Account;import com.fs.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.stereotype.Service;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;import javax.sql.DataSource;import java.util.List;/*    
*/@Servicepublic class AccountServiceImpl implements AccountService { //从ioc获取MyBatis动态代理的accountDao实现类 @Autowired private AccountDao accountDao; @Autowired private DataSource dataSource; @Override public List
findAll() { return accountDao.findAll(); } @Override public Account findAccountByName(String name) { return null; } /* ⚫ 使用spring提供的专用于mybatis的事务管理器在业务层硬编码进行事务管理 ⚫ 业务层要注入dataSource对象 //转账的业务实现 @Override public void transferMoneyAtoB(String aName, String bName, Integer money) { //先查出两个人的数据 Account aAccount = accountDao.findAccountByName(aName); Account bAccount = accountDao.findAccountByName(bName); //然后a减钱,b加钱 aAccount.setMoney(aAccount.getMoney()-money); bAccount.setMoney(bAccount.getMoney()+money); //编程调用事务 //开启事务,使用Spring的平台事务管理器,是一个接口,使用他的实现类构造器传递一个连接池 PlatformTransactionManager ptm = new DataSourceTransactionManager(dataSource); //创建事务定义对象 TransactionDefinition td = new DefaultTransactionDefinition(); //创建事务状态对象,用于控制事务执行 TransactionStatus ts = ptm.getTransaction(td); //然后调用转账方法(sql语句为更新账户) accountDao.transferMoney(aAccount); //制作一个异常 int i = 1/0; accountDao.transferMoney(bAccount); //提交事务 ptm.commit(ts); } */ //转账的业务实现 @Override public void transferMoneyAtoB(String aName, String bName, Integer money) { //先查出两个人的数据 Account aAccount = accountDao.findAccountByName(aName); Account bAccount = accountDao.findAccountByName(bName); //然后a减钱,b加钱 aAccount.setMoney(aAccount.getMoney()-money); bAccount.setMoney(bAccount.getMoney()+money); //然后调用转账方法(sql语句为更新账户) accountDao.transferMoney(aAccount); //制作一个异常 //int i = 1/0; accountDao.transferMoney(bAccount); }}

测试代码

package com.fs.service.impl;import com.fs.config.SpringConfig;import com.fs.pojo.Account;import com.fs.service.AccountService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {   "classpath:applicationContext.xml"})public class AccountServiceImplTestXML {       @Autowired    private AccountService accountService;    //测试查询所有方法    @Test    public void findAll() {           List
all = accountService.findAll(); System.out.println(all); } //测试转账方法 @Test public void transferMoneyAtoB() { //测试转账方法 accountService.transferMoneyAtoB("小付","小花",100); }}

transferMoneyAtoB()方法测试运行后数据库中结果

业务层转账方法正常运行成功后数据表数据

由于我们测试代码中是小付向小花转账100元,代码执行成功后应该小付900,小花1100

在这里插入图片描述
业务层转账方法我们给制作一个异常1/0 的/ by zero异常,
运存测试转账方法控制台输出
在这里插入图片描述
数据库中表的数据
小付原本900,小花1100,我们进行了转账业务,但是制作了异常,事务会进行回滚,所以金额不会发生变化

在这里插入图片描述

事务传播行为

在这里插入图片描述

Spring中的7个事务传播行为:

在这里插入图片描述

转载地址:http://kqsm.baihongyu.com/

你可能感兴趣的文章
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>