当前位置:网站首页>事务的四大特性
事务的四大特性
2022-06-27 13:35:00 【华为云】
什么是事务
事务是逻辑上的一组操作,要么都执行,要么都不执行。
经典案例
事务最经典也经常被拿出来说例子就是转账了。假如小明要给小红转账 1000 元,这个转账会涉及到两个关键操作就是:
- 将小明的余额减少 1000 元
- 将小红的余额增加 1000 元。
事务会把这两个操作就可以看成逻辑上的一个整体,这个整体包含的操作要么都成功,要么都要失败。这样就不会出现小明余额减少而小红的余额却并没有增加的情况。
什么是数据库事务
数据库事务在我们日常开发中接触的最多了。如果你的项目属于单体架构的话,你接触到的往往就是数据库事务了。
平时,我们在谈论事务的时候,如果没有特指分布式事务,往往指的就是数据库事务。
那数据库事务有什么作用呢?
简单来说:数据库事务可以保证多个对数据库的操作(也就是 SQL 语句)构成一个逻辑上的整体。构成这个逻辑上的整体的这些数据库操作遵循:要么全部执行成功,要么全部不执行 。
开启一个事务 START TRANSACTION; # 多条 SQL 语句 SQL1,SQL2… ## 提交事务 COMMIT;Copy to clipboardErrorCopied

另外,关系型数据库(例如:MySQL、SQL Server、Oracle 等)事务都有 ACID 特性:
什么是事务的ACID 特性呢
- 原子性(Atomicity) : 事务是最小的执行单位,不允许分割。事务的原子性确保动作要么全部完成,要么完全不起作用;
- 一致性(Consistency): 执行事务前后,数据保持一致,例如转账业务中,无论事务是否成功,转账者和收款人的总额应该是不变的;
- 隔离性(Isolation): 并发访问数据库时,一个用户的事务不被其他事务所干扰,各并发事务之间数据库是独立的;
- 持久性(Durabilily): 一个事务被提交之后。它对数据库中数据的改变是持久的,即使数据库发生故障也不应该对其有任何影响。
数据事务的实现原理呢?
我们这里以 MySQL 的 InnoDB 引擎为例来简单说一下。
MySQL InnoDB 引擎使用 redo log(重做日志) 保证事务的持久性,使用 undo log(回滚日志) 来保证事务的原子性。
MySQL InnoDB 引擎通过 锁机制、MVCC 等手段来保证事务的隔离性( 默认支持的隔离级别是 REPEATABLE-READ )。
保证了事务的持久性、原子性、隔离性之后,一致性才能得到保障。
边栏推荐
- On the complexity of software development and the way to improve its efficiency
- Debug tool
- 国产数据库乱象
- Array related knowledge
- 以前国产手机高傲定价扬言消费者爱买不买,现在猛降两千求售
- Infiltration learning diary day20
- Yyds dry goods inventory solution sword finger offer: cut rope (advanced version)
- A method to realize automatic renaming of pictures uploaded by WordPress
- Prometheus 2.26.0 新特性
- 类模板中可变参的逐步展开
猜你喜欢

抖音实战~公开/私密短视频互转

Journal quotidien des questions (6)

Number of printouts (solved by recursive method)

After 2 years of outsourcing, I finally landed! Record my ByteDance 3 rounds of interviews, hope to help you!

Implementation of recruitment website based on SSM

How to solve the problem of missing language bar in win10 system

《预训练周刊》第51期:重构预训练、零样本自动微调、一键调用OPT

PLM还能怎么用?

Realization of hospital medical record management system based on JSP

防火墙基础之华为华三防火墙web页面登录
随机推荐
Read a poem
Interviewer: do you understand redis' shared object pool?
crane:字典项与关联数据处理的新思路
类模板中可变参的逐步展开
A statistical problem of shell script
Implementing springboard agent through SSH port forwarding configuration
AXI总线
JVM performance tuning and monitoring tools -- JPS, jstack, jmap, jhat, jstat, hprof
Completely solve the problem of Chinese garbled code in Web Engineering at one time
创建Deployment后,无法创建Pod问题处理
Deep understanding of bit operations
Shake hands with life and make peace
Deploy redis sentinel mode using bitnamiredis Sentinel
基于JSP实现医院病历管理系统
What kind of air conditioner is this?
基于 xml 配置文件的入门级 SSM 框架整合
Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
Pytorch learning 1 (learning documents on the official website)
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
buuctf misc 百里挑一