How do ACID and database transactions work?

前端 未结 8 618
迷失自我
迷失自我 2020-11-29 14:31

What is the relationship between ACID and database transaction?

Does ACID give database transaction or is it the same thing?

Could someone enlighten this top

8条回答
  •  隐瞒了意图╮
    2020-11-29 15:22

    What is the relationship between ACID and database transaction?

    In a relational database, every SQL statement must execute in the scope of a transaction.

    Without defining the transaction boundaries explicitly, the database is going to use an implicit transaction which is wraps around every individual statement.

    The implicit transaction begins before the statement is executed and end (commit or rollback) after the statement is executed. The implicit transaction mode is commonly known as autocommit.

    As explained in this article, a transaction is a collection of read/write operations succeeding only if all contained operations succeed.

    Inherently a transaction is characterized by four properties (commonly referred as ACID):

    • Atomicity
    • Consistency
    • Isolation
    • Durability

    Does ACID give database transaction or is it the same thing?

    For a relational database system, this is true because the SQL Standard specifies that a transaction should provide the ACID guarantees:

    Atomicity

    Atomicity takes individual operations and turns them into an all-or-nothing unit of work, succeeding if and only if all contained operations succeed.

    A transaction might encapsulate a state change (unless it is a read-only one). A transaction must always leave the system in a consistent state, no matter how many concurrent transactions are interleaved at any given time.

    Consistency

    Consistency means that constraints are enforced for every committed transaction. That implies that all Keys, Data types, Checks and Trigger are successful and no constraint violation is triggered.

    Isolation

    Transactions require concurrency control mechanisms, and they guarantee correctness even when being interleaved. Isolation brings us the benefit of hiding uncommitted state changes from the outside world, as failing transactions shouldn’t ever corrupt the state of the system. Isolation is achieved through concurrency control using pessimistic or optimistic locking mechanisms.

    Durability

    A successful transaction must permanently change the state of a system, and before ending it, the state changes are recorded in a persisted transaction log. If our system is suddenly affected by a system crash or a power outage, then all unfinished committed transactions may be replayed.

    For more details about Durability and the Redo Log, check out this article.

提交回复
热议问题