> For the complete documentation index, see [llms.txt](https://wtifs.gitbook.io/diva-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wtifs.gitbook.io/diva-notes/db/my-sql/acid.md).

# ACID

**1. 原子性（Atomicity）**

* 原子性是指一个事务中的操作，要么全部成功，要么全部失败，如果失败，就回滚到事务开始前的状态。
* 通过什么机制来实现原子性？
  * undo log，事务失败时执行回滚

**2. 一致性（Consistency）**

* 一致性是指事务必须使数据库从一个一致性状态变换到另一个一致性状态，也就是说一个事务执行之前和执行之后都必须处于一致性状态。拿转账举例子，A账户和B账户之间相互转账，无论如何操作，A、B账户的总金额都必须是不变的。
* 通过什么机制实现一致性？
  * 其余的AID三个属性
  * 业务自己的逻辑保障。如A和B的金额存在两个库，那光凭数据库根本保证不了一致性，必须靠业务自己写逻辑

**3. 隔离性（Isolation）**

* 隔离性是指当多个用户并发的访问数据库时，如果操作同一张表，数据库则为每一个用户都开启一个事务，且事务之间互不干扰。
* 比如现有两个并发的事务 T1 和 T2，操作同一数据，T1 不应影响T2。
* 通过数据库锁（悲观锁、乐观锁） / 多版本实现

**4. 持久性（Durability）**

* 持久性就是指如果事务一旦被提交，数据库中数据的改变就是永久性的，即使断电或者宕机的情况下，也不会丢失提交的事务操作。
* 通过什么机制实现持久性？
  * 数据持久化
  * redo log


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wtifs.gitbook.io/diva-notes/db/my-sql/acid.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
