当前位置:网站首页>MYSQL transactions
MYSQL transactions
2022-08-01 01:10:00 【March is immortal】
Transactions
Transaction:
One or more SQL statements form an execution unit, which is either all executed or not executed at all
1, ACID attributes
Atomicity
Atomicity means that a transaction is an indivisible unit of work, and the operations in the transaction either all occur or none of them occur.
Consistency
The transaction must be in a consistent state before and after execution.For example, accounts a and b have a total of 1,000 yuan. After the transfer between the two people succeeds or fails, the sum of their accounts is still 1,000.
ISOLATION
Transaction isolation means that the execution of a transaction cannot be interfered by other transactions, that is, the operations and data used within a transaction are isolated from other concurrent transactions, and concurrently executed transactions cannot be mutuallyinterference.
Durability
Durability means that once a transaction is committed, its changes to the data in the database are permanent, and subsequent operations and database failures should not have any impact on it
2. Common concurrency issues
For multiple transactions running at the same time, when these transactions access the same data in the database, if the necessary isolation mechanism is not adopted, it will lead to various concurrency problems:
Dirty read: A transaction reads data that was not committed by another transaction
For two transactions T1, T2, T1 reads fields that have been updated by T2 but not yet committed. After that, if T2 rolls back, the content read by T1 is temporary and invalid.
Not repeatableRead: In the process of a transaction reading a row of data multiple times, another transaction modifies the data and commits it, resulting in different data read by the current transaction
For two transactions T1, T2, T1 reads a field, then T2 updates the field. After that, T1 reads the same field again, and the value is different.
Phantom read: When one transaction reads data in a certain range, another transaction inserts new records, causing the current transaction to read more data
For two transactions T1, T2, T1 reads a field from a table, then T2 inserts some new rows into that table. After that, if T1 reads the same table again, there will be morea few lines.
3. Isolation
Isolation of database transactions: The database system must have the ability to isolate and run various transactions concurrently, so that they will not affect each other and avoid various concurrency problems.
Isolation level: The degree to which a transaction is isolated from other transactions is calledIsolation level. The database specifies a variety of transaction isolation levels. Different isolation levels correspond to different interference levels. The higher the isolation level, the better the data consistency, but the weaker the concurrency
Set the isolation level to solve concurrency problems
The 4 transaction isolation levels provided by the database:
Isolation level
READ UNCOMMITTED
Allows transactions to read changes that have not been committed by other transactions, dirty reads,
Non-repeatable reads and phantom reads problems will occur
(If transaction 1 modifies the data and does not commit, transaction 2 reads data changesinto the modified data,
If transaction 1 is rolled back and transaction 2 reads the data again and becomes the original value [the problem of dirty read, non-repeatable read and phantom read]
READ COMMITTED
Only allow transactions to read changes that have been committed by other transactions [avoid dirty reads]
Dirty reads can be avoided, but non-repeatable reads and phantom reads still exist
(If transaction 1 modifies dataIf it is not committed, the data read by transaction 2 is the data that has been submitted, which can avoid dirty reading, but if the transaction is over, it will be committed,
The value read by transaction 2 again will be different, non-repeatable reading, and the problem of phantom reading still exists.will appear)
REPEATABLE READ
Ensure that a transaction can read the same value from a field multiple times. During the duration of the transaction, prohibiting other transactions from updating this field can avoid dirty reads and non-duplication, but the problem of phantom reads still exists
(If transaction 1 modifies the data, transaction 2 reads the original data [avoiding dirty reads], when transaction 1 commits, transaction 2 reads it again and avoids [repeatable read], only transaction 2After submitting, the data will be updated after other transactions are performed)
(The transaction modifies a certain column of the table [not yet carriage return], and the transaction inserts a column, when the transaction returns, it is found that the affectedIt is four lines, obviously there are three lines in the table,
hallucination problem)
SERIALIZABLE
Ensures that a transaction can read the same rows from a table, during the duration of the transaction, prohibit other transactions from performing insert, update and delete operations on the table,
All concurrency problems can be avoided, but performance is very slow
(Transaction 1 executes the modification operation, at this time the transaction 2 insert operation press Enter and find that it has been waiting, only after transaction 1 commits it will be executed)
Oracle supports 2 transaction isolation levels: READ COMMITED,
SERIALIZABLE.Oracle's default transaction isolation level is: READ
COMMITED
Mysql supports 4 transaction isolation levels. Mysql default transaction isolation level is: REPEATABLE READ
View the current isolation level: SELECT @@tx_isolation;
Set the isolation level of the current mySQL connection:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Set the global isolation level of the database system:
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED
边栏推荐
- pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
- 【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
- 【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
- High dimensional Gaussian distribution basics
- 一体化步进电机在无人机自动机场的应用
- [微服务]分布式事务解决方案-Seata
- 谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
- OSD读取SAP CRM One Order应用日志的优化方式
- [AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
- Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
猜你喜欢

【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO

机器学习初学者可以学哪些实战项目?

Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments

从零造键盘的键盘超级喜欢,IT人最爱

ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割

类和对象:中

Beijing suddenly announced that yuan universe big news

How to get started with YOLO?How to implement your own training set?

MYSQL经典面试题

元宇宙改变人类工作模式的四种方式
随机推荐
STK8321 I2C (Shengjia-accelerometer) example
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
JVM面试题总结(持续更新中)
SC7A20(士兰微-加速度传感器)示例
Detailed explanation of TCP protocol
Key Points Estimation and Point Instance
Js replication
WeChat applet page syntax
类和对象:上
GDB 源码分析系列文章五:动态库延迟断点实现机制
Rasa 3.x 学习系列-使用查找表改进实体提取
数据中台建设(七):数据资产管理
WindowInsetsControllerCompat is simple to use
C string array reverse
You need to know the TCP wave four times
What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
zeno使用方法笔记
RTL8762DK WDG(六)
Introduction to machine learning how to?