当前位置:网站首页>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
边栏推荐
- OSD read SAP CRM One Order application log way of optimization
- Rasa 3.x 学习系列-使用查找表改进实体提取
- Modern Enterprise Architecture Framework 1
- 【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
- Introduction to machine learning how to?
- 七月集训(第31天) —— 状态压缩
- 500 miles
- WebApi 打个Attribute 统一处理异常
- Academicians of the two academies speak bluntly: Don't be superstitious about academicians
- VPGNet
猜你喜欢
RTL8762DK Lighting/LED (3)
JVM面试题总结(持续更新中)
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
从零造键盘的键盘超级喜欢,IT人最爱
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
MVCC总结
Super like the keyboard made from zero, IT people love it
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
You need to know the TCP wave four times
leetcode:1648. 销售价值减少的颜色球【二分找边界】
随机推荐
pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
MYSQL事务
Introduction to the decision logic of WAASAP WebClient UI page labels
MYSQL索引解析
二叉树遍历非递归程序 -- 使用栈模拟系统栈
WindowInsetsControllerCompat is simple to use
What practical projects can machine learning beginners learn?
Compose原理-视图和数据双向绑定的原理
how to edit the table of contents of an epub ebook
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
欧拉系统(euleros):升级Mysql
Beijing suddenly announced that yuan universe big news
Rainbow share | how to use moving targets defense technology to guard against the unknown
机器学习应该如何入门?
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
cmake入门学习笔记
Classes and Objects: Above
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
Four ways the Metaverse is changing the way humans work
WeChat applet page syntax