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

另外,关系型数据库(例如:MySQL、SQL Server、Oracle 等)事务都有 ACID 特性:
什么是事务的ACID 特性呢
- 原子性(Atomicity) : 事务是最小的执行单位,不允许分割。事务的原子性确保动作要么全部完成,要么完全不起作用;
- 一致性(Consistency): 执行事务前后,数据保持一致,例如转账业务中,无论事务是否成功,转账者和收款人的总额应该是不变的;
- 隔离性(Isolation): 并发访问数据库时,一个用户的事务不被其他事务所干扰,各并发事务之间数据库是独立的;
- 持久性(Durabilily): 一个事务被提交之后。它对数据库中数据的改变是持久的,即使数据库发生故障也不应该对其有任何影响。
数据事务的实现原理呢?
我们这里以 MySQL 的 InnoDB 引擎为例来简单说一下。
MySQL InnoDB 引擎使用 redo log(重做日志) 保证事务的持久性,使用 undo log(回滚日志) 来保证事务的原子性。
MySQL InnoDB 引擎通过 锁机制、MVCC 等手段来保证事务的隔离性( 默认支持的隔离级别是 REPEATABLE-READ )。
保证了事务的持久性、原子性、隔离性之后,一致性才能得到保障。
本文内容到此结束了,
如有收获欢迎点赞收藏关注️,您的鼓励是我最大的动力。
如有错误疑问欢迎各位大佬指出。
主页: 共饮一杯无的博客汇总保持热爱,奔赴下一场山海。

边栏推荐
- Beginner level Luogu 1 [sequence structure] problem list solution
- 16 -- remove invalid parentheses
- Typescript learning materials
- What is the London Silver unit
- volatile与JMM
- [kotlin] the next day
- 洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
- Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
- Creation and use of static library (win10+vs2022
- 守护雪山之王:这些AI研究者找到了技术的新「用武之地」
猜你喜欢

LeetCode每日一练(无重复字符的最长子串)

PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文

Admixture usage document Cookbook

开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事

洛谷入门2【分支结构】题单题解
![Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp](/img/80/4edf21e0ac316fe3dd727159621acb.png)
Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp

substrate 技术每周速览 20220411

Problems encountered in vs compilation
![Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration](/img/65/413ac967cc8fc22f170c8c7ddaa106.png)
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration

Today, Teng Xu came out with 37k during the interview. It's really a miracle. He showed me his skill
随机推荐
Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
QT notes (XXVIII) using qwebengineview to display web pages
Keep valid digits; Keep n digits after the decimal point;
Why can't the start method be called repeatedly? But the run method can?
目前PolarDB-X是不支持数据库自制服务DAS么?
Weekly snapshot of substrate technology 20220411
Knowledge map model
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
Je veux acheter des produits à revenu fixe + mais je ne sais pas quels sont ses principaux investissements.
About tensorflow using GPU acceleration
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
LeetCode每日一练(杨辉三角)
Derivation of Halcon camera calibration principle
[issue 17] golang's one-year experience in developing Meitu
CentOS8-postgresql初始化时报错:initdb: error: invalid locale settings; check LANG and LC_* environment
Luogu_ P1007 single log bridge_ thinking
Hyperledger Fabric 2. X custom smart contract
E modulenotfounderror: no module named 'psychopg2' (resolved)
Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp