当前位置:网站首页>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 )!
边栏推荐
- 微信小程序父子组件之间传值
- Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange
- Acmstreamopen return value problem
- 什么是数据持久化?
- Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
- Read lsd-slam: large scale direct monolithic slam
- kenlm
- Coinlist queuing tutorial to improve the winning rate
- 升级cmake
- Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
猜你喜欢

SEO的5大关键指标:排名+流量+会话+停留时长+跳出率

acmStreamOpen返回值问题

Can Navicat directly operate the Android database SQLite

Hello CTP (IV) - CTP transaction API

Color NFT series representing diversity launched on the sandbox market platform

"Comment positionner l'industrie" dans la planification industrielle locale / parc

Basic use of OBS browser+ browser

Simple integration of client go gin 11 delete

Acmstreamopen return value problem

Summary of various problems encountered by cocos2d-x
随机推荐
Openmmlab environment configuration
BSC smart contract dividend mainstream currency | including marketing wallet | deflation | reflow | dividend free token | available for direct deployment
单元测试覆盖率
Laravel document sorting 9. Blade template
AI quantitative transaction (II) -- tushare financial data framework
Acmstreamopen return value problem
Cesium graphic annotation circle, square, polygon, ellipse, etc
PHP code audit 2 - these functions must be known and understood
Color NFT series representing diversity launched on the sandbox market platform
Hello CTP (IV) - CTP transaction API
numpy np tips: numpy数组的squeeze等处理
Lecture record: history and development of strapdown inertial navigation solution
Summary of various problems encountered by cocos2d-x
PHP code audit 1 - php Ini
[kubernetes series] installation and use of Helm
515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
A-table mouse over the display hand, the current line can be clicked
kenlm
95% 程序员都在这里摸鱼……
代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺