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

边栏推荐
- About tensorflow using GPU acceleration
- Numerical extension of 27es6
- OpenSSF安全计划:SBOM将驱动软件供应链安全
- Why can't the start method be called repeatedly? But the run method can?
- 我想買固收+產品,但是不了解它主要投資哪些方面,有人知道嗎?
- About sitemap XML problems
- Beginner level Luogu 1 [sequence structure] problem list solution
- 关于 SAP UI5 参数 $$updateGroupId 前面两个 $ 符号的含义
- 关于快速幂
- PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文
猜你喜欢

E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Keep valid digits; Keep n digits after the decimal point;

Teach you how to realize pynq-z2 bar code recognition

Admixture usage document Cookbook

CentOS8-postgresql初始化时报错:initdb: error: invalid locale settings; check LANG and LC_* environment

SQL parsing practice of Pisa proxy

Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment

#27ES6的数值扩展

一场分销裂变活动,不止是发发朋友圈这么简单!
随机推荐
Creation and use of static library (win10+vs2022
Teach you how to package and release the mofish Library
Jupiter core error
Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
请问阿里云实验中 k8s 对于3306端口转发,同时开启mysql客户端就会异常终止,是什么原因呢?
【170】PostgreSQL 10字段类型从字符串修改成整型,报错column cannot be cast automatically to type integer
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
E modulenotfounderror: no module named 'psychopg2' (resolved)
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
Is flutter easy to learn? How to learn? The most complete introduction and actual combat of flutter in history. Take it away without thanks~
Scrapy framework (I): basic use
Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references
Beginner level Luogu 2 [branch structure] problem list solution
分布式Session解决方案
LeetCode每日一练(无重复字符的最长子串)
Redis CacheClient
Difference between special invoice and ordinary invoice
Hyperledger Fabric 2. X custom smart contract
NFT dual currency pledge liquidity mining DAPP contract customization
Design of vga/lcd display controller based on FPGA (with code)