Transactions and concurrency
導入済み
6:35
データベース
A database is a set of true sentences about a domain. This course shows how to model, query, protect and govern data. The first three units model and shape it…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| transaction/trænˈsækʃn/ | トランザクション |
| atomicity/ətəˈmɪsɪti/ | 原子性 |
| consistency/kənˈsɪstənsi/ | 一貫性 |
| isolation/ˌaɪsəˈleɪʃn/ | 隔離性 |
| durability/ˌdjʊərəˈbɪlɪti/ | 持続性 |
| Serializability/ˈsɪərɪəlaɪzəbɪlɪti/ | The result equals some order of running the transactions one at a time. |
| Two-phase locking/tuː feɪz ˈlɒkɪŋ/ | a transaction takes all its locks before it releases any, which guarantees serialisable schedules |
| Isolation levels/ˌaɪsəˈleɪʃn ˈlevlz/ | settings that decide how much concurrent transactions may see of each other's changes |
| write-ahead log/raɪt əˈhed lɒɡ/ | a log in which every change is recorded before it is applied to the database |
| group commit/ɡruːp kəˈmɪt/ | writing several transactions' log records to disk in one operation |
| multiversion concurrency control/ˌmʌltɪˈvɜːʃn kənˈkʌrənsi kənˈtrəʊl/ | keeping several versions of each row so that each transaction reads a consistent snapshot and readers never block writers |
| write skew/raɪt skjuː/ | an anomaly where two transactions each read consistent data and write different rows, together breaking a rule neither broke alone |
| waits-for graph/weɪts fɔː ɡræf/ | a graph with an edge from each transaction to the one whose lock it waits for; a cycle means deadlock |
| dirty reads/ˈdɜːti riːdz/ | reading data written by a transaction that has not yet committed |
All or nothing
- A transaction 事务 happens completely or not at all. ACID names its four promises: atomicity 原子性 (all or nothing), consistency 一致性 (the constraints stay true), isolation 隔离性 (running side by side looks like running one after another) and durability 持久性 (a commit survives a crash).

The A in ACID stands for —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
All or nothing: the transaction commits completely or not at all.
The lost update, bank edition
- T1 reads 100, T2 reads 100, T1 writes 90, T2 writes 120. The withdrawal of 10 has vanished: the same race as in lesson 0703, now with money.
R1(A) R2(A) W2(A) W1(A)ends at 90, and T2's deposit is lost.R1(A) W1(A) R2(A) W2(A)ends at 110, the same as T1 then T2. Serializability 可串行化 means the result equals some order of running the transactions one at a time.
R1(A) R2(A) W2(A) W1(A) from 100 with -10/+20 ends —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
T1 writes last from its stale read: 100 - 10 = 90, the deposit lost.
Locks and levels
- Two-phase locking 两阶段锁 (2PL): a transaction takes all the locks it needs before it releases any (a growing phase, then a shrinking phase). That alone guarantees the outcome equals a serial order.
- Strict 2PL, used by most engines, also holds write locks until commit. The price is deadlock: the detector stops one transaction, which then tries again.
- Isolation levels 隔离级别 name the problems you accept. A dirty read 脏读: T2 sees T1's 90 before T1 commits, and T1 then rolls back. A changed re-read: the same row reads differently twice. A phantom: a re-run query finds new rows.
| Level (can it happen?) | Dirty read | Changed re-read | Phantom |
|---|---|---|---|
| read uncommitted | yes | yes | yes |
| read committed | no | yes | yes |
| repeatable read | no | no | yes |
| serializable | no | no | no |
- Keep transactions short: locks held while a user types invite deadlock.
Reading another transaction uncommitted rollback requires fixing to at least —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Read-committed shows only committed data - the dirty read ends there.
Order the isolation levels from weakest to strongest.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Each level removes one more anomaly: dirty reads, then unstable re-reads, then phantoms. Stronger levels lock or check more, so they cost more concurrency.
Recovery: the write-ahead log
- The write-ahead log 预写日志 rule: the log record for a change reaches disk before the changed page does, and a transaction commits when its commit record is on disk. After a crash, the engine redoes committed work and undoes the rest.
- Log: T1 begins, T1 sets A from $100$ to $90$, T2 begins, T2 sets B from $50$ to $70$, T1 commits, crash. Recovery redoes T1 ($A = 90$) and undoes T2 ($B = 50$).
- A log flush takes about $5$ ms, so group commit 组提交 puts several transactions in one flush: $10$ per flush gives $2000$ commits a second instead of $200$.
Log: T1 sets A 100 to 90; T2 sets B 50 to 70; T2 commits; crash. After recovery —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
T2 committed, so it is redone (B = 70); T1 did not, so it is undone (A = 100).
A log flush takes 5 ms and group commit puts 25 transactions in each flush. How many commits per second?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
200 flushes a second x 25 = 5000 commits a second.
Snapshots instead of read locks
- Multiversion concurrency control 多版本并发控制 (MVCC) maintains multiple versions of data so readers can see a consistent snapshot. Depending on the isolation level, the snapshot may be chosen per statement or per transaction; ordinary snapshot reads can often run alongside writes.
- Snapshot isolation still allows write skew 写偏斜. Two doctors on call each check that the other is on call, and both sign off. Each read was consistent, yet nobody is on call. Only serializable isolation stops this.
Snapshot isolation prevents every anomaly that serializable isolation does.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It allows write skew, as in the two doctors going off call.
Under MVCC, a long report query and an update to the same rows:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Each reads its own version; readers and writers do not block.
Worked example: break a deadlock cycle
- Under two-phase locking, T1 waits for T2, T2 waits for T3 and T3 waits for T1. The waits-for graph 等待图 has a cycle, so none of them can ever continue.
- Aborting any one transaction breaks the cycle: $\mathbf{1}$ abort is enough. The engine picks the cheapest victim, often the one that has done the least work, rolls it back and runs it again.
Waits-for cycle T1 → T2 → T3 → T1: fewest aborts that break it?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Removing any one node breaks the cycle.
Marks that slip away
- Trusting each statement on its own. Two transactions that read and then write can lose an update unless they are isolated.
- Releasing write locks early. Basic two-phase locking 两阶段锁 permits releases before commit after the growing phase; strict 2PL holds write locks until commit.
- A transaction open during user input. Its locks stay held while the user thinks, which invites deadlock.
Recap
- ACID: atomicity, consistency, isolation, durability.
- Serializable means equal to some serial order; two-phase locking 两阶段锁 guarantees it, at the price of deadlocks.
- Isolation levels name the problems you accept, from dirty reads to phantoms.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。