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

What is the transaction concept of database?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the transaction concept of database". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "what is the transaction concept of database"!

Business

What is the business?

Wiki:A transaction symbolizes a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions.

Database system concept: the set of operations that make up a single logical unit of work is called a transaction.

To put it simply, a transaction is a series of operations that contain some nature. Let's introduce these two points respectively.

A series of operations

The English of transaction is Transaction, transaction. This is the transaction in finance, the basic action of the transaction is to transfer money, and one of the most classic things is to transfer 50 yuan from A to B. This transaction is executed by the database with six lines of code:

Begin transaction (transaction start flag)

Read (A)

A = A-50

Write (A)

Read (B)

B = B + 50

Write (B)

End transaction (transaction end flag)

Read reads variables from disk to memory, + and-operations are memory operations, and write writes values in memory to disk.

Between the start and end flags of the transaction is a series of operations. It's just a piece of code, nothing special, so why put forward the concept of transaction? Then it comes to the nature of the transaction.

Atomicity (Atomicity)

Atomicity: all operations contained in a transaction are either performed or none are performed.

Why is it of this nature? For the transfer operation, the average user thinks that this is an action, which either happens or does not happen, and should not happen half of what has happened: the account of An is 50 less, but the account of B is not more than 50. Therefore, atomicity refers to the execution of a series of code for a transaction as one line of code.

What if there is a power outage in the middle of the execution? After the database is restarted, it needs to be restored to the state where the transaction is not executed, that is, rollback.

Consistency (Consistency)

Consistency: maintain database consistency when transactions are executed in isolation (when there is no concurrency).

This explanation is not very rogue.

Look at wiki's: Consistency ensures that a transaction can only bring the database from one valid state to another, maintaining database invariants: any data written to the database must be valid according to all defined rules, including constraints, cascades, triggers, and any combination thereof. This prevents database corruption by an illegal transaction, but does not guarantee that a transaction is correct.

Consistency means that the data in the database meets all the defined constraints of the database, including internal constraints on primary and foreign keys, as well as artificially defined constraints on two values, such as A+B=10 or the year of high school graduation must be 3% larger than the year of enrollment. It is the task of the transaction programmer to write a consistent transaction, and the database will do the final check, and the inconsistent transaction will be rejected by the database.

Vernacular: the database has its own rules, these rules are used to ensure that the state of the database is normal (consistent), transactions to execute, you need to follow the rules of the database.

Isolation (Isolation)

Isolation: the isolation of transactions refers to the system guarantee for two transactions T1 and T2 in concurrent execution. From the point of view of T1, T1 either completes before T2 starts or starts after T2 completion. So that the transaction does not feel that other transactions are executing concurrently in the system.

Isolation is related to scheduling, which is the order in which the code is executed. Consider that one action is that A transfers 50 to B, and the other is that B transfers 50 to A. Both actions are made up of six lines of code.

Under the premise of keeping the execution order of the internal code of the transaction unchanged, the database can have many scheduling strategies to execute these 12 lines of code. The strictest and most secure is serial scheduling, that is, one transaction at a time, and the serialization scheduling strategy for n transactions is n! Species is just an arrangement. Here are two serial schedules for two transactions.

It needs to be clear that different serial scheduling strategies may have different results, but all of them can meet the consistency. Think about why? Because every transaction satisfies consistency, one execution must be satisfied, no matter what order.

In addition to serial scheduling, there are many other scheduling, but the scheduling that meets the requirements should be equivalent to a serial scheduling, which is serializable scheduling. Consider the following two schedules:

The scheduling on the left is serializable, and the scheduling on the right is non-serializable.

Serializability is an isolation level. In order to meet the performance requirements, the database also provides other isolation levels, from strong to weak:

Serializable > repeatable read > read submitted > read uncommitted

Different isolation levels behave differently for database users. Isolating transactions according to a certain isolation level is the main goal of concurrency control.

Persistence (Durability)

Persistence: once a transaction is committed (commit), the state change to the corresponding data in the database should be permanent.

The commit of a transaction is similar to the preservation of word, which will not be lost after it is saved, so as to prevent the word from exploding.

To put it simply, the operation of the transaction to the data should be written to disk, not just in memory.

The intersection of four natures

The relationship between atomicity and consistency is difficult to understand.

Is atomicity a necessary condition for consistency? No, if the constraint of the database is that both An and B are greater than 0.

Begin transaction

Write (A) = 50

Write (B) = 50

End transaction

When write (A) is executed, write (B) is not executed, which is not atomic but consistent.

Is atomicity a sufficient condition for consistency? No, for the same constraint as above, An and B are both greater than 0, consider the following transaction

Begin transaction

Write (A) =-10

Write (B) =-20

End transaction

Even if the atomicity is satisfied and the values of An and B are changed, it is not consistent.

The relationship between isolation and consistency is simpler. Isolation is a necessary condition to ensure consistency when transactions are concurrent.

Atomicity, isolation and persistence are the common processing methods that the database needs to provide for all transactions. Only consistency is linked to the business. Their relationship is probably like the picture above. In this way, the basic definition and nature of the transaction are clear.

Thank you for your reading. the above is the content of "what is the transaction concept of database". After the study of this article, I believe you have a deeper understanding of what the transaction concept of database is. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report