数据库之事务


一、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:

  1. read(A)
  2. A := A – 50
  3. write(A)
  4. read(B)
  5. B := B + 50
  6. 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

2.并发冲突Concurrent Conflicts

(1)Write – Write conflict

(2)Read – write conflict

(3)Write – Read conflict

3.锁Lock

4. 2PL(Two-Phase Locking Protocol)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值