githubhtml
什么是本地事务(Local Transaction)?本地事务也称为数据库事务或传统事务(相对于分布式事务而言)。它的执行模式就是常见的:java
本地事务有这么几个特征:git
在讨论事务时,咱们绕不过一组概念:ACID,咱们来看看Wiki是怎么解释ACID的:github
Atomicity requires that each transaction be "all or nothing": if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible ("atomic"), and an aborted transaction does not happen.
关键词在于:数据库
The consistency property ensures that any transaction will bring the database from one valid state to another. Any data written to the database must be valid according to all defined rules, including constraints, cascades, triggers, and any combination thereof. This does not guarantee correctness of the transaction in all ways the application programmer might have wanted (that is the responsibility of application-level code), but merely that any programming errors cannot result in the violation of any defined rules.
一致性要求任何写到数据库的数据都必须知足于预先定义的规则(好比余额不能小于0、外键约束等),简单来讲就是在任什么时候间点都不能出现违反一致性要求的状态。并发
The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict - as opposed to relaxed - serializability), the effects of an incomplete transaction might not even be visible to another transaction.
隔离性要求若是两个事务修改同一个数据,则必须按顺序执行,而且前一个事务若是未完成,那么未完成的中间状态对另外一个事务不可见。oracle
The durability property ensures that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory.
持久性的关键在于一旦“完成提交”(committed),那么数据就不会丢失。app
在提到隔离性的时候咱们提到,在修改同一份数据的状况下,两个事务必须挨个执行以避免出现冲突状况。而数据库有四种隔离级别(注意:不是全部数据库支持全部隔离级别)分布式
Isolation Level | Dirty Reads | Non-Repeatable Reads | Phantom Reads |
---|---|---|---|
Read uncommitted | 容许 | 容许 | 容许 |
Read committed | 不容许 | 容许 | 容许 |
Repeatable reads | 不容许 | 不容许 | 容许 |
Serializable | 不容许 | 不容许 | 不容许 |
PS. 大多数数据库的默认隔离级别是Read committed。ide
来复习一下Dirty reads、Non-repeatable reads、Phantom reads的概念:
SELECT ... WHERE
获得一些行,B事务插入新行或者更新已有的行使得这些行知足A事务的WHERE
条件,A事务再次SELECT ... WHERE
结果比上一次多了一些行。大多数数据库在实现以上事务隔离级别(Read uncommitted除外)时采用的机制是锁。这也就是为何常常说当应用程序里大量使用事务或者高并发状况下会出现性能低下、死锁的问题。