当前位置:网站首页>mySQL transaction and its characteristic analysis
mySQL transaction and its characteristic analysis
2022-08-11 10:03:00 【The last three-legged beast】
Transactions
The core of a transaction is its atomicity. In computers, we make indivisible things atomic, and transactions are indivisible.
We often encounter the following situations:
A transfers $100 to B
The corresponding B will receive 100 yuan from A
These two things must either fail or complete. They are inseparable. If there is a sudden problem with the server after the transfer is successful, B does not receive 100 yuan, which will cause a big problem.The transaction provides a roll back operation for this situation, so that the data is returned before the transfer, which ensures the "prosperity of the data, and the loss of the data".
In addition to its core atomicity, transactions have three other properties:
- Persistence: that is, all changes made by the transaction will be stored in the hard disk and will not be changed due to server shutdown or restart
- Consistency: that is, when a transaction changes from one consistent state to another consistent state, "consistent" means that the data can be correct
- Isolation: In parallel development, transactions can be isolated and not interfere with each other.
Persistence is no longer described in detail.
Consistency
It is still the previous example of transferring money. If A transfers successfully but B does not receive the money, this is a transition from a consistent state to an inconsistent state.We can find that atomicity and consistency are closely linked.
Isolation
Although the definition of isolation is simple, it can be further studied according to the level of isolation.
Dirty read problem
A and B both process an account at the same time, if A sees that the account has 100, and then B adds 100 to the account, then dirty happensRead problem, that is, A read wrong data.In order to avoid this situation, the database software provides a lock mechanism. As long as B is modified to prohibit A from reading the data, the dirty read problem is avoided, that is, the write operation is locked, which reduces the concurrency, but slightly improves the accuracy of the result.Sex
The problem of non-repeatable reading
A and B are the same. If A is reading data, B suddenly modifies the data, which will cause A to read it again after reading it.The data was found to have changed, causing non-repeatability issues.For this problem, we can lock the read operation, that is, prohibit writing data when reading data.Doing this in turn reduces concurrency and further improves data accuracy.
The problem of phantom reading
It is still A and B. When A is reading data, B wants to add data, but what B wants to modify or add at this time is another file, which will not affectA is reading the data.In this way, although A reads the data multiple times and has no effect, there will be a situation where one more file is found while reading, which seems to have an illusion, but in most cases, this situation has no effect.To avoid the phantom read problem, parallel transactions must be serialized, that is, B cannot do it while A is doing it, and vice versa.This approach maximizes data accuracy while minimizing parallelism.
Database isolation mechanism
For different problems, mySQL provides four isolation levels:
- read uncommitted allows reading uncommitted data, that is, it is completely released, A can do whatever he wants, B can do whatever he wants, the highest degree of parallelism, and the lowest data accuracy
- read committed can only read the data after the commit, that is, lock the write operation to avoid the problem of dirty reading
- repeatable read locks read and write operations to avoid dirty and non-repeatable reads
- serializable is serialization, which maximizes data accuracy and minimizes parallelization, while avoiding dirty reads, non-repeatable reads, and phantom reads.
For different scenarios, we can choose different levels according to our needs. For example, the B-site like function does not need such high accuracy, and the lowest level restriction can be used, but for the transfer problem, a higher level of isolation means must be used..mySQL is the third level by default. If you need to modify it, you need to modify it in the configuration file.
边栏推荐
- 2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号
- 【无标题】超时超时超时超时超时
- 企业展厅制作要具备的六大功能
- HDRP shader to get shadows (Custom Pass)
- idea插件自动填充setter
- VideoScribe stuck solution
- CreateJS加速地址
- Have you encountered this kind of error? flink-sql writes to clickhouse
- What is the difference between the qspi interface and the ordinary four-wire SPI interface?
- Data middle platform program analysis and development direction
猜你喜欢
随机推荐
QTableWidget 使用方法
ES6:数值的扩展
使用.NET简单实现一个Redis的高性能克隆版(七-完结)
database transaction
Three handshakes and four waves
pycharm 取消msyql表达式高亮
Adobe LiveCycle Designer 报表设计器
【无标题】(完美解决)uni-app 小程序下拉刷新后刷新图标无法正常恢复的问题
Oracle database use problems
How to determine the neural network parameters, the number of neural network parameters calculation
Unity shader test execution time
The mathematical knowledge required for neural networks, the mathematical foundation of neural networks
数组、字符串、日期笔记【蓝桥杯】
How to use QTableWidget
保证金监控中心保证期货开户和交易记录
模型训练出现NAN
Primavera P6 Professional 21.12 Login exception case sharing
27岁了,目前从事软件测试,听些老一辈的人说测试前途是IT里最差的,是这样吗?
go基础之并发
MongoDB 非关系型数据库
![[UE] 入坑](/img/18/a329706541e45eb0db4bf3f7f99973.png)








