DatabaseTransaction refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that data-oriented resources are not updated permanently unless all operations in the transaction unit are successfully completed. You can combine a group of related operations into a unit that is either all successful or all failed to simplify error recovery.
Database Transaction refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that data-oriented resources are not updated permanently unless all operations in the transaction unit are successfully completed. You can combine a group of related operations into a unit that is either all successful or all failed to simplify error recovery.
Database Transaction refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that data-oriented resources are not updated permanently unless all operations in the transaction unit are successfully completed. By combining a set of related operations into a unit that either succeeds or fails, you can simplify error recovery and make the application more reliable. To become a transaction, a logical unit of work must meet the so-called ACID (atomicity, consistency, isolation, and durability) attributes.
Procedure
Imagine a transaction for online shopping. the payment process includes at least the following database operations:
I. update the inventory information of the products purchased by the customer
2. Save the customer's payment information, which may include interaction with the banking system
3. Generate the order and save it to the database
4. update user information, such as the number of shopping items
Under normal circumstances, these operations will go smoothly, the transaction will be successful, and all database information related to the transaction will be updated. However, if an error occurs in any part of the process, for example, an exception occurs when updating the inventory information, or the customer's bank account has insufficient deposits, the transaction will fail. Once a transaction fails, all the information in the database must remain unchanged before the transaction. For example, if the last step fails to update the user information, the transaction fails, make sure that the failed transaction does not affect the database status-the inventory information is not updated, the user has not paid, and the order has not been generated. Otherwise, the database information will be messy and unpredictable.
Database transactions are a technology used to ensure the stability and predictability of transactions in such circumstances.
Related attributes
Atomicity
(Atomic) (atomicity)
A transaction must be an atomic unit of work. modifications to its data must either be performed in all or not. Generally, operations associated with a transaction share a common goal and are mutually dependent. If the system executes only one subset of these operations, the overall goal of the transaction may be broken. Atomicity eliminates the possibility of a subset of system processing operations.
Consistency
(Consistent) (consistency)
When the transaction is completed, all data must be consistent. In related databases, all rules must be applied to transaction modifications to maintain the integrity of all data. At the end of the transaction, all internal data structures (such as B-tree indexes or two-way linked lists) must be correct. Some maintenance consistency responsibilities are borne by application developers who must ensure that the application has enforced all known integrity constraints. For example, when developing an application for transfer, do not move any decimal point during transfer.
Isolation
(Insulation) (isolation)
Modifications made by a concurrent firm must be isolated from those made by any other concurrent firm. The status of the data when the transaction is viewing the data is either the status before the transaction is modified or the status after the transaction is modified. The transaction does not view the data in the intermediate status. This is called isolation because it can reload the starting data and replay a series of transactions so that the State at the end of the data is the same as that at the execution of the original transaction. When a transaction is serializable, the highest isolation level is obtained. At this level, the results obtained from a group of parallel transactions are the same as those obtained by running each firm consecutively. Since high isolation limits the number of transactions that can be executed in parallel, some applications reduce the isolation level in exchange for greater throughput. Prevent data loss
Durability
(Duration) (durability)
After the transaction is completed, its impact on the system is permanent. This modification will remain even if a fatal system failure occurs.
Processing Model
There are three transaction models:
1. Implicit transactions refer to each data operation statement automatically becoming a transaction. Each transaction has an explicit start and end mark.
2. explicit transactions refer to transactions with explicit start and end tags. The start of a transaction is implicit and the end of a transaction is clearly marked.
3. Automatic transactions are automatically defaulted by the system, and start and end are not marked.
Concurrency Control
1. A major feature of the database system is that multiple users share database resources, especially multiple users can access the same data at the same time.
Serial control: if the transaction is executed sequentially, that is, after a transaction is completed, start another transaction.
Parallel Control: If the DBMS can accept multiple transactions at the same time, and these transactions can be executed in time overlap.
2. Concurrency Control Overview
Transactions are the basic unit of concurrency control. Ensuring ACID properties of transactions is an important task of transaction processing, and concurrent operations may destroy its ACID properties.
Responsibilities of the DBMS concurrency control mechanism:
Correct scheduling of concurrent operations ensures the isolation of transactions and Database Consistency.
If it is not locked and multiple users access a database at the same time, problems may occur when their transactions use the same data at the same time. Data inconsistency caused by concurrent operations includes: data loss and modification, "dirty" Data Reading (dirty reading), non-repeated reading, and ghost data.
(1) Modify lost data
When two or more transactions select the same row and update the row based on the originally selected value, the update will be lost. Every transaction does not know the existence of other transactions. The last update will overwrite the updates made by other firms, which will lead to data loss. For example.
For example, two editors have produced an electronic copy of the same document. Each editor independently changes its copy and saves the modified copy to overwrite the original document. Finally, the edited member who saves the change copy overwrites the changes made by the first edited member. This problem can be avoided if the second editor can make changes only after the first editor is complete.
(2) read "dirty" data (dirty read)
Reading "dirty" data means that transaction T1 modifies a data and writes it back to the disk. After transaction T2 reads the same data, T1, at this time, T1 restores the modified data to the original value. If the data read by T2 is inconsistent with that of the database, the data read by T2 is "dirty, that is, incorrect data.
For example, an editor is changing an electronic document. During the change process, another editor copied the document (this copy contains all changes made so far) and distributed it to the expected users. After that, the first editor thought that the change was incorrect, and deleted the edit and saved the document. Documents distributed to users include edited content that no longer exists, and such edited content should never be considered as existent. This issue can be avoided if no one can read the modified document before the first editor determines the final change.
(3) Non-repeated read
After transaction T1 reads data, transaction T2 performs an update operation, so that T1 cannot read the previous result. There are three cases of non-repeated reading:
After transaction T1 reads a certain data, T2. when T1 reads the data again, it gets a different value from the previous one.
(4) generate Ghost data
After reading some records from the database based on certain conditions, T2 deletes some of the records. When T1 reads data again based on the same conditions, it finds that some records disappear.
After T1 reads some data records from the database based on certain conditions, T2 inserts some records. When T1 reads data again based on the same conditions, it finds that there are more records.