事务 - 本地事务

曾宣
• 阅读 5659

事务 - 本地事务

github

什么是本地事务(Local Transaction)?本地事务也称为数据库事务传统事务(相对于分布式事务而言)。它的执行模式就是常见的:

  1. transaction begin
  2. insert/delete/update
  3. insert/delete/update
  4. ...
  5. transaction commit/rollback

本地事务有这么几个特征:

  1. 一次事务只连接一个支持事务的数据库(一般来说都是关系型数据库)
  2. 事务的执行结果保证ACID
  3. 会用到数据库锁

ACID

在讨论事务时,我们绕不过一组概念:ACID,我们来看看Wiki是怎么解释ACID的:

Atomicity 原子性

Atomicity requires that each transaction be "all or nothing": if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible ("atomic"), and an aborted transaction does not happen.

关键词在于:

  1. all or nothing,它的意思是数据库要么被修改了,要么保持原来的状态。所谓保持原来的状态不是我先insert再delete,而是压根就没有发生过任何操作。因为insert然后再delete实际上还是修改了数据库状态的,至少在数据库日志层面是这样。
  2. indivisible,不可分割,一个事务就是一个最小的无法分割的独立单元,不允许部分成功部分失败。

Consistency 一致性

The consistency property ensures that any transaction will bring the database from one valid state to another. Any data written to the database must be valid according to all defined rules, including constraints, cascades, triggers, and any combination thereof. This does not guarantee correctness of the transaction in all ways the application programmer might have wanted (that is the responsibility of application-level code), but merely that any programming errors cannot result in the violation of any defined rules.

一致性要求任何写到数据库的数据都必须满足于预先定义的规则(比如余额不能小于0、外键约束等),简单来说就是在任何时间点都不能出现违反一致性要求的状态。

Isolation 隔离性

The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict - as opposed to relaxed - serializability), the effects of an incomplete transaction might not even be visible to another transaction.

隔离性要求如果两个事务修改同一个数据,则必须按顺序执行,并且前一个事务如果未完成,那么未完成的中间状态对另一个事务不可见。

Durability 持久性

The durability property ensures that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory.

持久性的关键在于一旦“完成提交”(committed),那么数据就不会丢失。

数据库锁

在提到隔离性的时候我们提到,在修改同一份数据的情况下,两个事务必须挨个执行以免出现冲突情况。而数据库有四种隔离级别(注意:不是所有数据库支持所有隔离级别)

Isolation Level Dirty Reads Non-Repeatable Reads Phantom Reads
Read uncommitted 允许 允许 允许
Read committed 不允许 允许 允许
Repeatable reads 不允许 不允许 允许
Serializable 不允许 不允许 不允许

PS. 大多数数据库的默认隔离级别是Read committed。

来复习一下Dirty reads、Non-repeatable reads、Phantom reads的概念:

  • Dirty reads:A事务可以读到B事务还未提交的数据
  • Non-repeatable read:A事务读取一行数据,B事务后续修改了这行数据,A事务再次读取这行数据,结果得到的数据不同。
  • Phantom reads:A事务通过SELECT ... WHERE得到一些行,B事务插入新行或者更新已有的行使得这些行满足A事务的WHERE条件,A事务再次SELECT ... WHERE结果比上一次多了一些行。

大多数数据库在实现以上事务隔离级别(Read uncommitted除外)时采用的机制是锁。这也就是为什么经常说当应用程序里大量使用事务或者高并发情况下会出现性能低下、死锁的问题。

参考资料

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
OMG!Java高级开发岗必问知识点
目录1.Mysql2.CHAR与VARCHAR的区别?3.能说下myisam和innodb的区别吗?4.你能说下事务的基本特性和隔离级别吗?5.并发问题脏读、不可重复读、幻读?6.事务的隔离级别?7.说说自增主键、UUID?8.mysql的约束分类?9.drop、delete与tru
Easter79 Easter79
3年前
spring事务传播性(PROPAGATION)
spring事务传播性PROPAGATION\_REQUIRED简介加入当前已有事务;只有当前没有事务才启一个新的事务设计类ServiceA,方法methodA,事务级别定义为PROPAGATION_REQUIRED类ServiceB,方法method
Wesley13 Wesley13
3年前
MySQL 空事务
   问题描述;   研发同事反应MySQL数据库有锁,检查innodb\_trx时,发现有很多长时间未结束的空事务。   这些事务的trx\_mysql\_thread\_id都为0,因此不能通过kill  id的方式强制关闭这些长时间未结束的僵尸事务。           SELECT       tr
Wesley13 Wesley13
3年前
MySQL数据库InnoDB存储引擎Log漫游(1)
作者:宋利兵来源:MySQL代码研究(mysqlcode)0、导读本文介绍了InnoDB引擎如何利用UndoLog和RedoLog来保证事务的原子性、持久性原理,以及InnoDB引擎实现UndoLog和RedoLog的基本思路。00–UndoLogUndoLog是为了实现事务的原子性,
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Easter79 Easter79
3年前
Spring支持多数据源的@Transactional事务注解
1、配置事务注解驱动、每个数据源对应的事务管理器,并定义“限定符”<tx:annotationdriven/    <bean id"transactionManager1" class"org.springframework.jdbc.DataSourceTransactionManager"  ... 
Wesley13 Wesley13
3年前
Spring事务管理
Spring提供了一流的事务管理。在Spring中可以支持声明式事务和编程式事务。    本章主要目标如下:     1,Spring事务    2,事务属性    3,事务管理器    4,声明式事务      1.1Spring的事务     事务管理在应用程序中起着至关重要的作用:它是一系列任务
线上SQL超时场景分析-MySQL超时之间隙锁 | 京东物流技术团队
前言之前遇到过一个由MySQL间隙锁引发线上sql执行超时的场景,记录一下。背景说明分布式事务消息表:业务上使用消息表的方式,依赖本地事务,实现了一套分布式事务方案消息表名:mqmessages数据量:3000多万索引:createtime和statuss
Spring事务实现原理
1、引言spring的springtx模块提供了对事务管理支持,使用spring事务可以让我们从复杂的事务处理中得到解脱,无需要去处理获得连接、关闭连接、事务提交和回滚等这些操作。spring事务有编程式事务和声明式事务两种实现方式。编程式事务是通过编写代
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
曾宣
曾宣
Lv1
仍怜故乡水,万里送行舟。
文章
4
粉丝
0
获赞
0