当前位置:网站首页>MySQL transactions
MySQL transactions
2022-08-05 10:01:00 【Jiangnan has no ancestors】
事务的概念
Transactions are related by a set of logicDML语句(数据库操纵语言)组成,MySQLProvide a mechanism for these statements to either all execute successfully,要么全部执行失败.
A complete transaction needs to satisfy four properties:
原子性、一致性、隔离性、持久性
简称ACID:
原子性(Atomicity)
一致性(Consistency)
隔离性(Isolation)
持久性(Durability)
Transactions serve the application layer,There is no need for database users to think about a lot of potential bugs and concurrency issues.
MySQL使用 Innodb 引擎才支持事务 .
show engines \G --查看数据库引擎
事务的属性
原子性:
一个事务中的所有操作,要么全部完成,要么全部不完成.事务在执行过程中发生错误,Need to roll back to the state before the transaction started.
持久性:
事务结束后,对数据的修改是永久的,Even if the system fails, it will not be affected.
隔离性:
A database allows multiple transactions to read and write data concurrently,Isolation prevents data inconsistencies caused by the cross-execution of multiple transactions.
一致性:
事务执行前后,数据库完整性没有被破坏.The first three properties are guaranteed to achieve consistency,Of course, consistency also requires database users to operate properly.
事务的提交
The submission method is manual submission、自动提交
InnoDB的单条sqlStatements are encapsulated as transactions,Affected by autocommit
查看是否自动提交
关闭自动提交:
SET AUTOCOMMIT=0;
开启自动提交:
SET AUTOCOMMIT=1;
手动提交:
输入begin或者start transaction来开启一个事务
It is not affected by autocommit after it is turned on,必须通过commit提交,data is persistent
When the operation is abnormal,MySQL自动回滚,You can set the rollback point to roll back manually,commitAfter rollback does not work.
设置回滚点:
savepoint 回滚点名称;
rollback to 回滚点名称;
rollback; --Roll back to the beginning
事务的隔离级别
脏读:A transaction reads uncommitted data
幻读:A transaction reads newly inserted data
不可重复读:A certain data read before and after a transaction is different
读未提交【Read Uncommitted】:
所有的事务都可以看到其他事务没有提交的执行结果.
Equivalent to no isolation,也会有很多并发问题,如脏读,幻读,不可重复读.
读提交【Read Committed】 :
The default isolation level for most databases(不是 MySQL 默认的).一个事务只能看到其他的已经提交的事务所做的改变.这种隔离级别会引起不可重复读.
可重复读【Repeatable Read】:
MySQL 默认的隔离级别,它确保同一个事务,在执行中,Multiple reads have the same result.但是会有幻读问题(MySQLThis level solves the phantom read problem).
串行化【Serializable】:
事务的最高隔离级别,对所有操作全部加锁,But serialization causes timeouts and lock contention,效率很低,几乎不使用.
查看全局隔离级别:
SELECT @@global.tx_isolation;
查看当前会话隔离级别:
SELECT @@session.tx_isolation;
SELECT @@tx_isolation;
Set the current or global isolation level:
set session/global transaction isolation level 隔离级别;
总结:
隔离级别越严格 ,安全性越高,但并发性越差
MySQL 读未提交 存在脏读、幻读、不可重复读的问题,
read commit exists 幻读、 不可重复读的问题.
边栏推荐
- Oracle temporary table space role
- mysql索引
- 还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
- 【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
- Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
- The difference between find, matches, lookingAt matching strings in matcher
- 语音社交软件开发——充分发挥其价值
- Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
- Happens-before rules for threads
- 上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
猜你喜欢
随机推荐
ffmpeg drawtext 添加文本水印
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
无题二
IDEA performs the Test operation, resulting in duplicate data when data is inserted
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
leetcode: 529. Minesweeper Game
C语言的高级用法
无题十
攻防世界-PWN-new_easypwn
19. Server-side session technology Session
Why are RELTABLESPACE values 0 for many tables displayed in sys_class?
华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
[强网杯2022]WP-UM
What is the function of the regular expression replaceFirst() method?
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
你最隐秘的性格在哪?
力扣(LeetCode)216. 组合总和 III(2022.08.04)
【温度预警程序de开发】事件驱动模型实例运用
What is CRM Decision Analysis Management?
Handwriting Currying - toString Comprehension