文章目录
一、transaction事务
1.事务的概念
A transaction is a unit of program execution that accesses and possibly updates various data items.(一系列访问和可能修改数据的程序执行)
2.事务的五种状态
- Active:初始状态和当它处于执行状态时
- Partially committed:在最终的状态被执行后
- Failed:发生错误不能再正常执行
- Committed:成功的执行完成后
- Aborted:在事务回滚(roll back)和数据库恢复优先的状态开始新的事务后,之后要么结束事务,要么重新开始事务。
3.事务的四种性质(ACID属性)
(1)ACID
英文 | 汉语 | 含义 |
---|---|---|
Atomicity | 原子性 | 要么都没有做,要么都完成 |
Consistency | 一致性 | 隔离执行事务时要保证一致性 |
Isolation | 隔离性 | 事务之间只能看到未完成与完成两种状态 |
Durability | 持久性 | 事务完成后,对系统的影响是永久的 |
(2)ACID需求
Transactions access data using two operations:
- read(x)
- write(x)
一个事务例子:
Transaction to transfer $50 from account A to account B:
- read(A)
- A := A – 50
- write(A)
- read(B)
- B := B + 50
- write(B)
四种需求
-
Atomicity requirement:
例子:fails after step 3 and before step 6,意思是A减少了50,但B还没有加50。我们要求数据库恢复时要么所有操作都执行完,要么都什么也没有做。
解释:不一致性是一定存在的,但数据库需要保证不一致状态只能在事务执行当中存在。
处理:核心是在磁盘上保存旧值,transaction-management component(事务管理器) -
Consistency requirement
解释:执行后A和B的总和不变。
处理:确保一致性是应用程序员(Application programmer)的责任。 -
Isolation requirement:
例子:在3到6步之间,有另一个事务并发修改数据
解释:应该平凡地(trivially)确保串行地(serially)执行多个事务(一个在另一个之后)。
处理:concurrency-control component(并非控制器) -
Durability requirement:
解释:事务完成后立即更新数据库
处理:充分记录操作过程,能够恢复出更新过程,recovery-management component(恢复管理器)
(3)例题
-
“Once a transaction committed, changes to database must be persistent”, indicates which property of transaction :Durability。
-
“When a transaction is failed in the middle of its execution, the modification that has been made to the database by this transaction before failure time must be aborted to guarantee the consistency of database”, indicates which property of transaction: ( Consistency )
当事务在执行过程中失败时,必须中止该事务在失败时间之前对数据库所做的修改,以确保数据库的一致性。一致性是指所有的修改最终使总和不变,但现在只修改了一部分,另一部分还没有被修改。 -
“A transaction should be ended either by commit or abort”, indicates which property of transaction ?( Atomicity )
原子性的概念 -
在失败前被提交正确部分应该被重做(redo),未执行的错误部分要撤销(undo)
二、并发控制Concurrency Control
1.并发执行Concurrent Executions
Advantages:
increased processor and disk utilization,
reduced average response time