当前位置:网站首页>事务的四大特性
事务的四大特性
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 )。
保证了事务的持久性、原子性、隔离性之后,一致性才能得到保障。
本文内容到此结束了,
如有收获欢迎点赞收藏关注️,您的鼓励是我最大的动力。
如有错误疑问欢迎各位大佬指出。
主页: 共饮一杯无的博客汇总保持热爱,奔赴下一场山海。

边栏推荐
- 固收+产品有什么特点?
- #28对象方法扩展
- Redis CacheClient
- Does polardb-x currently not support self-made database service Das?
- All you want to know about large screen visualization is here
- MySQL中符号@的作用
- Scrapy framework (I): basic use
- PSS: you are only two convolution layers away from the NMS free+ point | 2021 paper
- 28 object method extension
- 一场分销裂变活动,不止是发发朋友圈这么简单!
猜你喜欢

CNN convolutional neural network (the easiest to understand version in History)

Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!

VS编译遇到的问题
![Beginner level Luogu 1 [sequence structure] problem list solution](/img/60/5e151ba31eb00374c73be52e3bfa7e.png)
Beginner level Luogu 1 [sequence structure] problem list solution

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

Derivation of Halcon camera calibration principle

【kotlin】第二天

#27ES6的数值扩展

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

Programming skills: script scheduling
随机推荐
Volatile and JMM
Redis CacheClient
CAS comparison and exchange
Design of direct spread spectrum communication system based on FPGA (with main code)
Atomic operation class
Introduction to TTCAN brick moving
Knightctf 2022 web section
I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
专用发票和普通发票的区别
Admixture usage document Cookbook
R language error
Hyperledger Fabric 2. X custom smart contract
Beginner level Luogu 1 [sequence structure] problem list solution
Design of electronic calculator system based on FPGA (with code)
PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
All you want to know about large screen visualization is here
About fast exponentiation
Jupiter core error
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
关于TensorFlow使用GPU加速