Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition of transactions in database

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "the definition of transactions in the database". In the daily operation, I believe that many people have doubts about the definition of transactions in the database. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the definition of transactions in the database"! Next, please follow the editor to study!

Suppose user A wants to transfer 1000 yuan to B from his account, then two steps are needed: first, subtract 1000 yuan from A's account, and then add 1000 yuan to B's account. In either of these two steps, either steps can be completed successfully or nothing can be done, and the errors should be rolled back to where they were before the transfer started. Such a process is what we often talk about. Based on this example, let's define the transaction as follows:

Definition of transaction

Transaction is an indivisible logical unit composed of a series of data operations or instructions, which are either executed or not executed in order to ensure the consistency and integrity of data. If the transaction executes successfully, the changes made to all data in the transaction become persistent changes in the database; if errors are encountered during the execution of the transaction, all actions and changes performed should be cancelled and rolled back to the state before the transaction execution. By definition, a transaction should satisfy four attributes, namely ACID, namely atomicity, consistency, isolation, and persistence.

Atomicity (Atomicity, or indivisibility): either all operations in a transaction are performed successfully or none of them are completed. This uninterruptible series of operations is atomicity. In the process of transfer, account A minus 1000 yuan and account B plus 1000 yuan must be executed or not, otherwise they are not in line with logic and reality.

Consistency: before the transaction starts and after the transaction is completed, all relevant data states are consistent and the integrity of the database is not compromised. Atomicity will ensure that the data is consistent, and the transfer process is interrupted. The first step is executed, but the second step is not carried out. At this time, the An account is missing 1000 yuan, while the B account does not add 1000 yuan. At this time, the data is inconsistent.

Isolation (also known as independence): when multiple concurrent transactions in a database are executed at the same time, the execution of one transaction cannot be disturbed by other transactions, that is, the operations and data used in one transaction are isolated from other transactions and will not be affected by other concurrent transactions. For example, when a transaction looks at the state of the data, it is either the state before the modification of another concurrent transaction, or the state after the modification of another transaction, which will not be the data of the intermediate state.

Durability: after the transaction is executed, the modification to the data is permanent, and even if the system fails, there will be no rollback and the execution result will not be affected.

The running mode of the transaction

Typically, transactions run in three modes: autocommit transactions, explicit transactions, and implicit transactions.

1. Automatically commit transactions

Each individual SQL statement automatically commits the transaction after its execution, that is, the COMMIT operation is automatically implicitly executed immediately after the execution of the SQL statement. If an error occurs, the transaction rolls back to the previous state.

two。 Explicit transaction

It begins by specifying a transaction start statement to explicitly open a transaction, and a piece of code that ends with a commit command or a rollback command to commit or roll back a transaction is a user-defined explicit transaction.

3. Implicit transaction

In implicit transactions, there is no need to use BEGINTRANASACTION to open a transaction, and each SQL statement starts a transaction the first time it executes until commit [transaction] is used to commit or rollback [transaction] to end the transaction.

In ORACLE, MYSQL, and SQLSERVER, these modes are turned on and off in different ways.

At this point, the study of "the definition of transactions in the database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report