事务,是在数据库中用于保证数据正确性的一种机制,涉及到很多概念以及不同的情况,这里做一个总结

相关概念#

事务四特性(ACID)

原子性(Atomicity,或称不可分割性):要么全部完成或者全部不完成,错误是会发生回滚,这个要求两条相关的操作,就像情侣一样,要么一起海誓山盟,一个发生意外,另外一个也挂了

一致性(Consistency):操作完成后和原本想的结果一样,不能对数据完整性造成破坏

隔离性(Isolation,又称独立性):可以同时开启多个事务,为避免出现错误,针对这个问题制定了事务在处理时对数据进行不同隔离

持久性(Durability):事务操作后数据永久存储数据库。

事务并发问题

脏读:事务A读取了事务B更新的数据,然后B回滚操作,那么A读取到的数据是脏数据,数据是错误的就是脏数据

不可重复读:事务A多次读取同一数据,事务B在事务A多次读取的过程中,对数据作了更新并提交,导致事务A多次读取同一数据时,结果不一致。

幻读:一个事务进行数据处理,正在处理时插入一条记录,原本事务处理后发现一条记录没有更改,如同发生幻觉一般。

spring事务机制

spring针对事务进行一系列的处理

事务隔离级别

在多个事务进行处理的时候锁住数据,保证统一时间只有一个事务对数据进行处理,针对不同的情况有不同的锁

DEFAULT(默认值):使用底层数据库事务隔离级别,MySql中使用select @@tx_isolation可以查询当前隔离级别

READ_COMMITTED:读已提交,读到已经提交的数据,可以防止脏读,但是对不可重复读和幻读

READ_UNCOMMITTED:读未提交,可以读取没有提交的数据,较少用

REPEATABLE_READ:重复读取,读出去后自动加锁,其他事务不能修改,解决脏读,不可重复读

SERIALIZABLE:串行化,事务一个排一个执行,一个事务执行完成后执行下一个

事务传播机制

在服务中进行数据库操作,两个操作中事务如何管理

REQUIRED(默认值):如果当前没有事务开启一个事务,有的话自动加入到这个事务中

REQUIRES_NEW :针对被调用者,不管调用者是否存在事务,被调用者创建一个新事务。

MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常。

NESTED:如果存在事务,嵌套事务中执行,如果没有则新建

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

代码实现

1),这里是基于注解的方式使用事务,使用之前引入aop,tx,context约束

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd

2),配置事务管理器

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

3),开启事务注解

<tx:annotation-driven/>

 <!-- 设置基于基于接口或者基于类的代理被创建 -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 不配置的话可能造成类型转换错误-->

整体配置如下

<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 配置mybatis数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SQLSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:resource/mybatis-config.xml" />
<!-- 配置SQL映射文件信息 -->
<property name="mapperLocations">
<list>
<value>classpath:com/bdqn/dao/*.xml</value>
</list>
</property>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bdqn.dao" />
</bean> <!-- 声明DaoBean,添加注解,开启自动扫描 -->
<context:component-scan base-package="com.bdqn.service"></context:component-scan> <!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:annotation-driven/> <!-- 设置基于基于接口或者基于类的代理被创建 -->
<aop:aspectj-autoproxy proxy-target-class="true"/> </beans>

4),处理服务类

@Service("userService")
public class UserServiceImpl implements UserService{ @Autowired
private UserDao userDao; // 一个普通数据库Dao层,进行修改数据操作 // 注意@Transactional配置propagation指定隔离范围,rollbackFor指定发生什么情况回滚
@Transactional(propagation=Propagation.REQUIRED,rollbackFor= {NullPointerException.class})
public int updateNames() {
userDao.updateName("李1","19");
/*if(true) { // 测试回滚的时候取消注释
throw new NullPointerException();
}
userDao.updateName("王1","20");*/
return 0;
}
}

5),测试类

public static void main(String[] args){
ApplicationContext ac = new ClassPathXmlApplicationContext("resource/spring.xml");
UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService"); userService.updateNames();
}

最新文章

  1. SQL Server数据阻塞原因
  2. java并发编程学习:用 Semaphore (信号量)控制并发资源
  3. 国内如何使用gem?
  4. oracle rac自动生成awr
  5. [ALM]一步一步搭建MS ALM环境 - 安装域服务器
  6. ELK+FileBeat+Log4Net搭建日志系统
  7. mysql基础语法之(全文索引)
  8. 火狐谷歌浏览器Json查看插件
  9. docker on Mac
  10. React问答小demo
  11. httpclient post请求实例(自己写的)
  12. 浅谈HTML之模仿人人网登陆界面(新手必学)
  13. WinRarHelper帮助类
  14. [Unity Socket]在Unity中如何实现异步Socket通信技术
  15. Spring定时器实现(二)
  16. jQuery 学习03——HTML:捕获、设置、添加元素、删除元素、CSS类、CSS()方法、尺寸
  17. mysql的多表查询join
  18. 娓娓道来c指针 (7)指针运算
  19. Netty源码分析第2章(NioEventLoop)----&gt;第4节: NioEventLoop线程的启动
  20. 01_Storm体系概要

热门文章

  1. CAD交互绘制带颜色宽度的直线(网页版)
  2. 二分 || UOJ 148 跳石头
  3. MySQL-07 日志管理
  4. 关于Error:Maven&#160;Resources&#160;Compiler:&#160;Maven&#160;project&#160;configuration&#160;required&#160;for&#160;module&#160;&#39;项目名&#39;&#160;isn&#39;t&#160;available.&#160;Compilation&#160;of&#160;Maven&#160;projects&#160;is&#160;supported&#160;only&
  5. svn 设置代理
  6. curl 模拟post请求
  7. MySQL数据类型与操作
  8. STM32——输入捕获实验原理及配置步骤
  9. 算法导论 第三章 and 第四章
  10. 在WinForm里嵌入WPF模拟公交运行状态