文章目录
事务并发可能出现的问题
Lost updates problem
一个事件的write操作,会覆盖掉另一个事件已经执行的write操作。
Successfully completed update is overridden by another user.
Uncommitted updates problem
这个可以看作是我们在别的地方看到的“脏读”。
也就是一个事务去更新,或者查询了另外一个还没提交的事物更新过的数据。因为另外一个事物还没提交,所以它随时可能会rollback(回滚)。
Occurs when one transaction can see intermediate results of another transaction before it has committed.
Incorrect analysis problem
Occurs when a transaction reads several values from the database but a second transaction updatessome of them duringthe execution of the first
这个可以看作是我们在别的地方看到的“不可重复读”。
事务A可能第一次查询到A值,那么它可能希望的是在事务执行期间,如果多次查询数据,都是同样的一个A值,它希望这个A值是它重复读取的时候一直可以读到的。它希望这行数据的值是可重复读的。
e.g.
- 事务T6在计算账户X(100元), Y(50元), Z(25元)的总数。
- 同时,事务T5从账户X里转了10元到账户Z里。
那么这时,事务T6就会得出错误结论(多算了10元)。
Concurrency Control Techniques
conservative approaches
加锁
(1). 如果事务T请求读一个数据项,并且没有其他的事务拥有这个数据项上的写锁,那么控制就把那个数据项上的读锁授予T,允许操作继续进行。注意,因为其他事务可能在那时拥有该数据项上的读锁,所以读锁(read lock)常常也称为共享锁(shared lock)。
(2). 如果事务T请求读一个数据项,但是另一个事务T‘拥有那个数据项上的写锁,那么T必须等到T’文成(并释放它的锁)为止。我们称这个读请求与之前批准的写请求冲突(conflict)。
(3). 如果事务T请求写一个数据项,并且没有其他的事务再那个数据项上拥有读锁或者写锁,那么控制就把那个数据项上的写锁授予T,允许操作继续进行。因为写锁排斥了所有其他的锁,所以也常常称之为排他锁(exclusive lock)。
(4). 如果事务T请求写一个数据项,但是另一个事务T‘拥有那个数据项上的读锁或写锁,那么T必须等到T’文成(并释放它的锁)为止。我们称这个写请求与之前批准的写请求冲突。
(5). 一旦授予事务锁,事务就拥有了这个锁。数据项上的读锁允许事务对那个数据项进行读。数据项上的写锁允许事务对那个数据项进行读或写。当事务完成时,它释放授予它的所有锁。
2PL (two-phase locking)
2PL的问题
- Cascading rollback,
- Deadlocks: timeout deadlock prevention.
Timestamping
optimistic methods
Assume confilct is rare.
At commit, a check is made to determine whether conflict hahs occurred. If there is a conflict, transaction must be rolled back and restarted.
Transactions and Recovery
Recovery manager responsible for atomicity and durability.
- If failure occurs between commit and database bufers being flushed to secondary storage then, to ensure durability, recovery manager has to redo (rollforward) transaction’s updates.
- If transaction had not committed at failure time, recovery manager has to undo (rollback) any effects of that transaction for atomicity.
Checkpointing
When failure occurs, redo all transactions that committed since the chockpoint and undo all transactions active at time of crash.