Interview Question: Tell me about your understanding of database transactions

When answering this question, the first thing to consider is what knowledge points are contained in it that we want to answer,
the first point: what is a transaction;
the second point: what are the basic characteristics of the database?
The third point: what is transaction isolation and what are the transaction isolation levels?

What is a transaction
A transaction in a database is a series of operations performed as a single logical unit of work. (Multiple SQL statements, either all execute successfully or all fail.)
To support transaction operations, the database must meet four characteristics, which is often referred to as ACID:
A: Atomicity: Atomicity refers to the contents of the transaction. All operations either all succeed or all fail
C: Consistency: A transaction must be in a consistent state before and after execution.
I: Isolation: Multiple transactions cannot be interfered by other transactions while performing the same operation.
D: Durability: Once a transaction is committed, its changes to the data in the database should be permanent.
What is transaction isolation and what are the transaction isolation levels?
Transaction isolation means that when multiple concurrent transactions access a database at the same time, one transaction should not be interfered by another transaction, and each concurrent transaction should be isolated from each other.

A general database includes the following four isolation levels:
Read Uncommitted,
Read Committed,
Repeated Read,
Serializable

Read Uncommitted
means that you can read uncommitted content.

Therefore, under this isolation level, the query will not be locked, and because the query is not locked, the consistency of this isolation level is the worst, which may result in "dirty reads" and "non-repeatable reads". ", "phantom reading".

Read Committed
Read commit is to read the content that has been submitted.

This is the most commonly used isolation level in various systems and is the default isolation level for SQL Server and Oracle. This isolation level can effectively avoid dirty reads , unless the lock is displayed in the query. "Read commit" can only avoid "dirty reads", but not "non-repeatable reads" and "phantom reads".

Repeated Read
Repeated read is an isolation level specially formulated for the problem of "non-repeatable read", which can effectively avoid "non-repeatable read". and it is alsoMySql's default isolation level

When a transaction starts, "update" is not allowed, and "non-repeatable read" is precisely because the data is modified between two reads. Therefore, "repeatable read" can effectively avoid "non-repeatable read". Repeated reads", but "phantom reads" cannot be avoided, because phantom reads are generated due to "Insert or Delete" operations.

Serializable
The highest isolation level of the database, at this level, transactions are "serialized and executed sequentially", that is, they are executed one by one (each row of data is locked).

At this level, "dirty read", "non-repeatable read", and "phantom read" will not appear, but the execution efficiency is very poor and the performance overhead is the largest, so basically no one will use it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324128114&siteId=291194637