当前位置:网站首页>Thorough understanding of database transactions
Thorough understanding of database transactions
2022-06-25 04:28:00 【Life is sweet and good luck is good】
Reprint : Understanding of database transactions
Business
Business (Transaction), Generally speaking, it refers to the things to be done or done . In computer terminology, a program execution unit that accesses and may update various data items in a database (unit). In computer terminology , Transactions usually refer to database transactions .
Concept
A database transaction usually contains a sequence of operations to read or write to the database . Its existence includes the following two purposes :
1、 It provides a way for database operation to recover from failure to normal state , At the same time, it provides a method to keep the consistency of database even in abnormal state .
2、 When multiple applications are accessing the database concurrently , You can provide an isolation method between these applications , To prevent the operation of each other from interfering with each other .
When a transaction is committed to DBMS( Database management system ), be DBMS You need to make sure that all operations in the transaction are completed successfully and that the results are persisted in the database , If some of the operations in the transaction did not complete successfully , Then all operations in the transaction need to be rolled back , Go back to the state before the transaction ( Or do it all , Or none of them ); meanwhile , This transaction has no effect on the execution of database or other transactions , All transactions seem to be running independently .
But in reality , The risk of failure is high . During the execution of a database transaction , There may be transaction operation failure 、 Database system / The operating system failed , Even storage media failure . This requires DBMS Perform a recovery operation on a failed transaction , Restore its database state to a consistent state ( The state in which the consistency of data is guaranteed ). In order to realize the function of restoring the database state to a consistent state ,DBMS It is often necessary to maintain transaction logs to track all operations in transactions that affect database data .
characteristic
Not any sequence of operations on a database is a database transaction . The transaction should have 4 Attributes : Atomicity 、 Uniformity 、 Isolation, 、 persistence . These four attributes are often called ACID characteristic .
Atomicity (Atomicity): The transaction is executed as a whole , The operations on the database contained in it are either all performed , Either not .
Uniformity (Consistency): Transactions should ensure that the state of the database changes from one consistent state to another . Consistent state means that the data in the database should meet the integrity constraints .
Isolation, (Isolation): When multiple transactions are executed concurrently , The execution of one transaction should not affect the execution of other transactions .
persistence (Durability): Once a transaction is committed , His changes to the database should be permanently stored in the database .
give an example
Use a common “A Account to B Account remittance ” To show how to ensure the accuracy and integrity of data through database transactions . Familiar with relational database transactions from the account A To account number B need 6 Operations :
1、 from A Read the balance in the account number (500).
2、 Yes A Subtract the account number (500-100).
3、 Write back the results A Account number (400).
4、 from B Read the balance in the account number (500).
5、 Yes B Add the account number (500+100).
6、 Write back the results B Account number (600).
Atomicity :
Guarantee 1-6 All processes are either carried out , Either not . Once there is a problem in the process of performing a certain step , You need to perform a rollback operation . If you go to step five ,B The account is suddenly unavailable ( Like being logged off ), Then all previous operations should be rolled back to the state before the transaction is executed .
Uniformity
Before the transfer ,A and B There are 500+500=1000 Yuan . After the transfer ,A and B There are 400+600=1000 element . in other words , The state of data changes from one state to another after the transaction operation . At the same time, consistency can ensure that the account balance will not become negative .
Isolation,
stay A towards B The whole process of transfer , As long as the transaction has not been committed (commit), Inquire about A Accounts and B Account time , The amount of money in both accounts will not change .
If in A to B At the same time of transfer , There's another transaction going on C to B Transfer operation , So when both transactions are over ,B The money in the account should be A Transfer to B Plus C Transfer to B Plus your own money .
persistence
Once the transfer is successful ( Transaction submission ), The money in two accounts will really change ( Will write the data to the database for persistence )!
边栏推荐
- "Grammar sugar" -- my new programming knowledge
- 2021.4.15 note the difference between let, const and VaR in ES6
- 小白学习MySQL - 统计的'投机取巧'
- SQL注入详解
- 【LeetCode】22. 括号生成
- 讲座记录《惯性导航的新应用——惯性测量》
- 1. Phase II of the project - user registration and login
- Summary of various problems encountered by cocos2d-x
- ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
- 如何筛选出和产品相关的词,精准排除掉无效词
猜你喜欢

"Grammar sugar" -- my new programming knowledge

Color NFT series representing diversity launched on the sandbox market platform

1. first knowledge of chromatic harmonica

UCLA | 用于黑盒优化的生成式预训练

PHP code audit 1 - php Ini

如何绘制产业招商地图

无法安装redis接口

讲座记录《惯性导航的新应用——惯性测量》

Lecture record: data processing methods and applications of various spatial geodetic techniques

Hello CTP (III) - CTP quotation API
随机推荐
关于TCP连接四次握手(或者叫四次挥手)的详细总结
General steps for QT compiling database plug-ins
1280_ C language to find the average value of two unsigned integer
什么是数据持久化?
How many images can opencv open?
Numpy NP tips: squeeze and other processing of numpy arrays
Laravel document sorting 2. Route related
LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
Laravel document sorting 9. Blade template
Color NFT series representing diversity launched on the sandbox market platform
SQL, CTE, FLG CASE问题
Flutter Builder & FutureBuilder组件
UCLA | generative pre training for black box optimization
Standing wave ratio calculation method
Error 1062 is reported during MySQL insertion, but I do not have this field.
Flutter FittedBox组件
Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
关于TCP连接三次握手的详细总结
"Renaissance" in the digital age? The bottom digital collection makes people happy and sad
Hello CTP (IV) - CTP transaction API