数据库系统原理 14章 Transaction

Transaction Concept

事务是数据库执行中的一个执行单元,通常是用于保持数据的一致性的,保证在数据执行过程中的所有语句要么全部执行要么全都不执行.
在事务执行前后数据库是一致的。

  1. A transaction is a unit of program execution that accesses and possibly updates various data items.
  2. A transaction must see a consistent database.
  3. During transaction execution the database may be temporarily inconsistent.
  4. 事务执行的过程中会出现短暂的不一致现象,当事务成功的执行后数据库又会恢复到一致的状态。when the transaction completes successfully(is committed), the database must be consistent. when the transaction completes successfully( is committed), the database must be consistent.
  5. 当一个事务提交之后数据库中的数据即是永久的,对数据库的更新是永久存在的After a transaction commits, the changes it has made to the database persist, even if there are system failures.(宕机、磁盘损坏都不会影响数据库内容)
  6. Multiple transaction can execute in parallel .影响数据一致性的操作是insert update等更新操作。当我们执行一个更新语句的时候事务会自动产生,但是事务的提交是人为提交的。
  7. Two main issues to deal with:
    [1] Failures of various kinds, such as hardware failures and system crashes软硬件错误都不会对数据库内容造成影响
    [2] Concurrent execution of multiple transactions 可以进行多个语句的并发操作

事务执行过程中的4个属性

A transaction is a unit of program execution that accesses and possibly updates various data items. To preserve the integrity of database system must ensure:

  1. 原子性Atomicity:当前执行单元的所有语句全部执行成功或者全部执行失败
  2. 一致性Consistency:保证数据库执行前后是一致的
  3. 独立性Isolation:数据库本身是一个多用户的,多个用户同时对数据库进行操作的时候,多个事务是独立的,不会对其他的用户产生影响,每一个用户使用的时候都是独占的感觉,事务之间相互不干扰。T1提交之后T2才能看到T1执行的结果,T2不知道T1的执行
  4. 永久性Durability:一旦一个事务成功执行之后,对于数据库的更新是永久的无论成功还是失败。

在这里插入图片描述

  • Atomicity requirement - if the transaction fails after step3 and before step6, the system should ensure that its updates are not reflected in the database, else an inconsistency will result. 任何在step3和6之间断了的操作都需要进行回滚。恢复到最原始的状态
  • Consistency requirement - the sum of A and B is unchanged by the executive of the transaction. A和B的总和不变
  • Isolation requirment - if between step3 and 6, another tarnsaciton is allowed to access the partially updated database, it will see an inconsistent database ( the sum A+B will be less than it should be). Isolation can be ensured trivially by running transactions serially, that is one after the other. However, executing multiple transactions concurrently has significant benefits, as we will see later.事务执行到Step3到6之间的时候,假设在这个时候对A和B账户执行的总和进行查询,查询到的是对当前事务执行之前的A和B的账户总额,不会查询到事务查询到一半一个变了一个没变的总和,保证事务执行的过程中其他的操作不会读到中间结果。
  • Durability requirement - once the user has been notified that the transaction has completed, the updates to the database by the transaction must persist desperate failures. 当事务step6执行之后A就变成了永久-50 ,B永久+50

在这里插入图片描述
事务的开始是隐含的,但是事务的结束是有标志的,一个是提交工作一个是回滚, 即中间过程操作失败的情况。

Transaction State

事务可以看成是一段程序,看成对数据库的一系列更新操作。

  1. Active
    the initial state; the transaction stays in this state while it is executing.事务在初始化完成之后,事务正在执行
  2. Partially committed
    半提交状态,有一部分语句已经执行完毕
  3. Failed
    当前的某一个语句执行失败了
  4. Aborted
    事务要进行rollback
    事务是一个完整性的单元,这个单元的数据或都执行成功或都执行失败。只要中间有一个语句或者有某几个语句执行失败,就应该放弃这个事务。
  5. Committed
    当所有的语句执行成功之后即可提交

在这里插入图片描述
出现错误的结局一定是aborted。主要是看执行过程中是否有语句出现问题。

Implementation of Atomicity and Durability

The recovery-management component of a database system implements the support for atomicity and durability.
如何保证数据库的原子性和永久性?
给出了一种可恢复的管理机制,可能出现部分提交的状态或者失败的状态,这个时候就需要rollback到原始的状态,这个时候就需要对原始的状态进行保留。

  • 可恢复管理机制:事务在执行过程中,可能出现部分提交状态或失败状态,如果出现失败状态,要将部分提交的数据回滚到原始状态。(即使所有语句都执行成功了,但是最终的结果并不是理想结果也可以进行放弃,全部rollback)
  • The shadow-database scheme(影子数据库机制):
    【1】一个事务处于激活状态的时候,创建一个影子数据库
    【2】a pointer called db_pointer always points to the current consistent copy of the database
  • 影子数据库存在前提:
    【1】假设磁盘是没有错误的,磁盘上有足够的空间可以存放原有数据和新数据,不会因为磁盘的错误造成数据的错误
    【2】useful for text editors, but extremely inefficient for large databases ( why? ). Does not handle concurrent transactions?不适用于并发事务!
    在这里插入图片描述
    如果进行rollback的话,可以随时指向原有数据和新的数据。


⚠️在影子数据库操作的时候一定要注意一个问题:虽然只是移动了指针,但是注意只是一个用户进行移动,但是如果是多用户同时操作的话一旦将指针移动到了新库上,意味着其他用户对数据库的操作就失效了。

Concurrent Executions

并发操作。在数据库当中多个事务是同时进行的,数据库本来就是一个多用户、多并发的系统。并发可以提高系统执行效率和设备利用率。
Multiple transactions are allowed to run concurrency in the system, Advantages:

  1. Increased processor and disk utilization, leading to better transaction throughout: one transaction can be using the CPU while another is reading from or writing to the disk.交替的进行占用CPU 和IO
  2. reduced average response time,短时的事务优先,提高平均等待时间。

Schedules

调度指的是一定的指令,一个调度包含了一个事务的全部指令。一个调度当中不会存在一个事务只执行了一半。在调度当中必须保证所有的指令都能依次被执行到,对于每一个事务的指令都是顺序执行的。在执行调度的过程中,每个事务的指令在各自的顺序保持不变。
再次强调,调度包含了一个或多个事务,包含了事务的全部操作指令。

Example

串行化调度

两个事务的执行没有交叉
在这里插入图片描述

在这里插入图片描述

虽然调度1和调度2最后的结果不一样,但是A和B账户的总和是一致的。

并发调度

在这里插入图片描述

和调度1结果一样

在这里插入图片描述
不对不对,没有保持数据的一致性。
事务的调度有专门的并发控制器进行的。

Serializability

  1. 可串行化调度,Basic Assumption: 前提是每一个事务都能保持一致性。任何一个事务单独执行的时候都能一致。

  2. 既然每个事务都能保持数据的一致性,那么串行事务也能够保持数据的一致性。
    如果一个调度是并行的能够通过一个转换方式将其转换为一个串行调度的话就说明它是可以保证事务的一致性的。

在这里插入图片描述

1. Conflict serializability

冲突可串行化
冲突指令:两个指令如果发生冲突,当且仅当同时对一个Q进行操作,至少一个进行写操作。
在这里插入图片描述

如果是不冲突的指令,两条指令的次序可以进行交换;如果是冲突的指令不能交换。

如果一个调度可以通过一系列不冲突指令的转换,转换成完全可以串行调度,则称为冲突可串行化调度。转换前后是冲突等价的。
在这里插入图片描述

2. View serializability

视图可串行化

  1. For each data item Q, if transaction Ti reads the initial value of Q in schedule S, then transaction Ti must, in schedule S’, also read the initial value of Q.如果之前先读了Q的初始值,之后也要读。
  2. For each data item Q if transaction Ti executes read(Q) in schedule S, and that value was produced by transaction Tj(If any), then transaction Ti must in schedule S’ also read the value of Q that was produced by transaction Tj.如果在Ti读Q之前有其他事务对他进行写操作,也要保证顺序。
  3. 保证写回去的操作一致。对于写操作如果是最后一条的话,对应的转换之后的写操作也要是最后一条。

在这里插入图片描述
在这里插入图片描述
这里对于写操作采用盲写的机制来保证,即只保证第一个读的仍然是第一个读,最后一个写的还是最后一个写,中间的靠盲写的机制来保证。

其他可串行化操作方式

在这里插入图片描述

Determining such equivalence requires analysis of operations other than read and write.
既不是冲突可串行化也不是视图可串行化,但是这个的结果AB的两个总和仍然没有改变。

Testing for Serializability

串行化的判别
如果当前的一个调度是串行化调度的话就能保持一致性,当有多个调度的时候如何判定当前的调度方式是一致的。

  1. Consider some schedule of a set of transactions T1, T2,…, Tn.
  2. Precedence graph(优先图) - a. direct graph where the vertives are the transactions(names).
    We draw an arc from TI to Tj if the two transaction conflict, and TI accessed the data item on which the conflict arose earlier.
    We may label the arc by the item that was accessed.
    在优先图中画出各个事务对事项的优先操作次序。比如T1对数据项x先执行T2对数据项x后执行,画出一个由T1指向T2的箭头,表示一个优先次序。
    在这里插入图片描述
    如果T1和T2是冲突可串行化的,则在优先图中是没有环路的。而在这个例子当中出现环路则当前调度不是冲突可串行化调度。
一个较复杂的例子

X只有T2操作所以可以放在任何位置,不用考虑。对于Y的话T1、T2、T4均有操作,需要对三者之间的顺序有一个较好的排列,T1和T4之间也要有arc指向。

对于T5中的读和写操作在前面的4个数据项中都没有操作,所以可以认为T5是孤立的操作, 不受前面的操作影响。可以没有任何的关联。T5单独拿出作为一个孤立的结点即可。
然后根据precedence graph给出一个可行的串行顺序即可。

在这里插入图片描述


Summary
  1. A schedule is conflict serializable if and only if its precedence graph is acyclic.
  2. Cycle-detection algorithms exists which take order n2 time, where n is the number of vertices in the graph.( Better algorithms take order n+e where e is the number of edges.)
  3. If precedence graph is acyclic, the serializability order can be obtained by a topological sorting of the graph.
    将无环图变换成一个线性的可串行化调度的执行流程根据这个次序就可以说明当前的调度是冲突可串行化的。而且冲突可串行化的次序是不唯一。
视图可串行化与优先图

视图可串行化问题不能够通过优先图解决,视图可串行化的判定是一个NP问题。
在这里插入图片描述

Recoverable Schedules

可恢复调度:如果在这个调度中Ti读取了Tj的数据,就应该在提交的时候先提交Ti的数据再提交Tj的数据。
在这里插入图片描述
上图中如果T9先于T8提交的话,那么T9就不是一个可恢复调度,因为在T9提交之后,可能T8发现了错误之后进行了rollback,那这个时候T9读到的是T8roolback之前的数据,结果出现了错误。

Cascading Rollbacks

Cascading rollback (级联回滚) - a single transaction failure leads to a series of transaction rollbacks.(由于某一个事务的rollback造成其他一系列事务同时rollback。)
在这里插入图片描述
上图如果T10rollback,那么后面的T11和T12都需要进行rollback。
这种级联回滚会导致数据库中的大量工作变成无效工作,这种情况是不希望发生的。
所以希望每个调度都不是级联的调度。
Cascadeless schedules - cascading rollbacks cannot occur : for each pair of transactions TI and Tj such that TI reads a data item previously written by TI, the commit operation of TI appears before the read operation of Tj.
如果Tj 读取了Ti 写的数据,则Ti要在Tj写之前得到提交。二者实际上是可恢复的调度,如果全部都是可恢复的话,可以说这个调度是非级联的。如何进行判别?

Concurrency Control

在数据库中有相应的并发控制策略。A database must provide a mechanism thar will ensure thar all possible schedules are :
【1】either conflict or view serializable保证所有的都是冲突可串行化或者是视图可串行化的
【2】are recoverable and preferably cascadeless或者保证是可恢复的
【3】are serial schedules recoverable/cascadeless

需要这样一种机制来保证当前的调度是可串行化或者是可恢复的。——并发控制策略
通过一系列的事先判别来保证当前的调度是可串行化的并且是不需要通过事后画优先图来进行验证的。

  • Concurrency-control protocols allow concurrent schedules, but ensure that the schedules are conflict/view serializable , and are recoverable and cascade less.
  • Concurrency control protocols generally do not examine the precedence graph as it is being created.(Instead a protocols imposes a discipline that avoids nonseralizable schedules.)

不同的并发控制策略是有不同的代价的,一些预先检测的策略会有一些额外的时间或者空间上的开销,而事后的冲突可串行化判别是并发控制策略是否正确。实际上在整个的数据库当中使用并发控制策略进行预先的检测,避免产生不可串行化的调度。

  • Different concurrency control protocols provide different tradeoffs between the amount of concurrency they allow and the amount of overhead that they incur.
  • Tests for serializability help us understand why a concurrency control protocol is correct.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值