InnoDB和ACID特性

2021-08-05

https://dev.mysql.com/doc/refman/8.0/en/mysql-acid.html

The ACID model is a set of database design principles that emphasize aspects of reliability that are important for business data and mission-critical applications.
 

A: atomicity.原子性

Transactions are atomic units of work that can be committed or rolled back. When a transaction makes multiple changes to the database, either all the changes succeed when the transaction is committed, or all the changes are undone when the transaction is rolled back.

一个事务要么成功提交,要么全部失败回滚,不能只执行其中部分操作。

C: consistency.一致性

The database remains in a consistent state at all times — after each commit or rollback, and while transactions are in progress. If related data is being updated across multiple tables, queries see either all old values or all new values, not a mix of old and new values.

一个事务提交后、回滚后或者正在执行中,别的query不管在哪个阶段,获取到的数据(另一个事务修改的这部分数据) 只能 全部都是新的或者全部都是旧的数据。

事务的执行不能破坏数据库数据的完整性和一致性,一个事务在执行之前和执行之后,数据库都必须处于一致性状态。

如果数据库系统在运行过程中发生故障,有些事务尚未完成就被迫中断,这些未完成的事务对数据库所作的修改有一部分已写入物理数据库,这是数据库就处于一种不正确的状态,也就是不一致的状态

I: isolation.隔离性

Transactions are protected (isolated) from each other while they are in progress; they cannot interfere with each other or see each other's uncommitted data. This isolation is achieved through the locking mechanism. Experienced users can adjust the isolation level, trading off less protection in favor of increased performance and concurrency, when they can be sure that the transactions really do not interfere with each other.

通过锁机制,并发的事务是相互隔离的,一个事务的执行过程不应被其他的事务干扰。即便有多个并发事务共同操作同一块数据,也应有合适的机制保证数据不会紊乱。
标准SQL中定义了4个事务隔离级别,不同的隔离级别对事务的处理不同:读未提交,读已提交(授权读取),可重复读,串行化。

15.7.2.1 Transaction Isolation Levels

https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html
 

D: durability.持久性

The results of transactions are durable: once a commit operation succeeds, the changes made by that transaction are safe from power failures, system crashes, race conditions, or other potential dangers that many non-database applications are vulnerable to. Durability typically involves writing to disk storage, with a certain amount of redundancy to protect against power failures or software crashes during write operations. (In InnoDB, the doublewrite buffer assists with durability.)

一旦事务提交了,事务所更改的数据会永久的保存在数据库中,及时断电或者系统崩溃后。