当前位置:网站首页>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 )!
边栏推荐
- Flutter Builder & futurebuilder components
- 如何绘制产业招商地图
- 1. Phase II of the project - user registration and login
- The 5th series of NFT works of missing parts was launched on the sandbox market platform
- Basic use of OBS browser+ browser
- Failed to install redis interface
- 【LeetCode】143. Rearrange linked list
- 什么是存储引擎以及MySQL常见的三种数据库存储引擎
- Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
- 515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
猜你喜欢

警惕超范围采集隐私-移动APP违规十宗罪

Hello CTP (II) -- Introduction to CTP

1. Phase II of the project - user registration and login

小白学习MySQL - 统计的'投机取巧'

Cesium loading display thermal diagram

MySQL插入过程报错1062,但是我没有该字段。

Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'

navicat可不可以直接操作安卓数据库SQLite

论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》

Shutter fittedbox component
随机推荐
Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
Text keyword extraction: ansj
SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
PHP code audit 1 - php Ini
WMS仓储管理系统的使用价值,你知道多少
Cesium loading display thermal diagram
Laravel document sorting 2. Route related
Doubts about judging the tinyint field type of MySQL
CMD operation MySQL in Windows
Hello CTP (I) - basic knowledge of futures
Acmstreamopen return value problem
【LeetCode】148. Sort linked list
How many images can opencv open?
mysql的tinyint字段类型判断的疑惑
Failed to install redis interface
什么是数据持久化?
Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
小白学习MySQL - 统计的'投机取巧'
Upgrade cmake
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。