当前位置:网站首页>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 幻读、 不可重复读的问题.
边栏推荐
- 【温度预警程序de开发】事件驱动模型实例运用
- 阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
- three物体围绕一周呈球形排列
- Handwriting Currying - toString Comprehension
- eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
- 19.服务器端会话技术Session
- Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
- 上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
- NowCoderTOP35-40——持续更新ing
- 长达四年的减肥记录
猜你喜欢

Two-table query average grouping in sql server

语音社交软件开发——充分发挥其价值

mysql索引

Microservice Technology Stack

入门 Polkadot 平行链开发,看这一篇就够了

Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)

First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad

shell脚本实例

js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)

【温度预警程序de开发】事件驱动模型实例运用
随机推荐
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
创建一个 Dapp,为什么要选择波卡?
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
ffmpeg drawtext add text watermark
Advanced usage of C language
入门 Polkadot 平行链开发,看这一篇就够了
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
攻防世界-PWN-new_easypwn
一文道清什么是SPL
NowCoderTOP35-40 - continuous update ing
公众号如何运维?公众号运维专业团队
hcip BGP 增强实验
Oracle temporary table space role
无题一
手把手教你纯c实现异常捕获try-catch组件
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
mysql索引
MySQL advanced (twenty-seven) database index principle