1 Transactions Qin Xu 4-323A Life Science Building, Shanghai Jiao Tong University Tel: (O) Webpage:
2 Webpage for DBMS
3 Textbook and References 1 Abraham Silberschatz, Henry F. Korth, S. Sudarshan. Database System Concepts. 5 th edition. McGraw-Hill Companies. ( 杨 冬 青, 马 秀 莉, 唐 世 渭, 等 译 数 据 库 系 统 概 念 机 械 工 业 出 版 社 ) 2 Jeffrey D. Ullman, Jennifer Wildom. A First Course in Database Systems. Dept. of CS, Stanford University 3 王 珊, 萨 师 煊 数 据 库 系 统 概 论 高 等 教 育 出 版 社
4 Course Outline Ch1. Introduction Section 1: Relational database ( 关 系 型 数 据 库 ) Ch2. Relational model ( 关 系 模 型 ) Ch3. SQL ( 结 构 化 查 询 语 言 ) Ch4. Advanced SQL Ch5. Other Relational Query Languages Section 2: Database design ( 数 据 库 设 计 ) Ch6. Database Design: Entity-Relationship Model ( 实 体 - 关 系 模 型 ) Ch7. Relational Database Design ( 关 系 数 据 库 设 计 ) Ch8. Application Design ( 应 用 程 序 设 计 )
5 Course Outline (cont.) Section 3: Data storage query ( 数 据 存 储 与 查 询 ) Ch9. Storage and File Structure ( 存 储 与 文 件 结 构 ) Ch10. Indexing and Hashing ( 索 引 与 哈 希 ) Ch11. Query Processing ( 查 询 处 理 ) Ch12. Query Optimization ( 查 询 优 化 ) Section 4: Transaction Management ( 事 务 管 理 ) Ch13. Transaction ( 事 务 概 念 ) Ch14. Concurrency Control ( 并 发 控 制 ) Ch15. Recovery system ( 系 统 恢 复 ) Section 5: Object-Based Databases and XML ( 面 向 对 象 数 据 库 ) Ch16. Object-Based Databases Ch17. XML Ch18. Data mining ( 数 据 挖 掘 )
6 Syllabus Lecture + Discussion Computer Labs Group-based Project Lab rotation PRP Thesis Final Exam (written) 32 hours 16 hours > 10 hours 2 hours
7 Scoring Policy Assignments 10%+10% Computer Lab Reports 20% Group projects 10%-10% Final 60%
8 Transactions ( 事 务 ) Transaction Concept & Properties( 事 务 的 概 念 和 特 性 ) Transaction State ( 事 务 的 状 态 ) Implementation of Atomicity and Durability( 实 现 原 子 性 和 持 久 性 ) Concurrent Executions( 并 发 执 行 )and implementation of Isolation( 实 现 隔 离 性 ) Serializability ( 可 串 行 化 ) Recoverability ( 可 恢 复 性 ) Testing for Serializability ( 可 串 行 化 判 定 )
9 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction must see a consistent database. During transaction execution the database may be temporarily inconsistent. When the transaction completes successfully (is committed), the database must be consistent. After a transaction commits, the changes it has made to the database persist, even if there are system failures. Multiple transactions can execute in parallel. Two main issues to deal with: Failures of various kinds, such as hardware failures and system crashes Concurrent execution of multiple transactions
10 ACID Properties preserve the integrity of data the database system must ensure: Atomicity( 原 子 性 ) Either all operations of the transaction are properly reflected in the database or none are. Consistency( 一 致 性 ) Execution of a transaction in isolation preserves the consistency of the database. Isolation( 隔 离 性 ) Although multiple transactions may execute concurrently, each transaction must be unaware of other concurrently executing transactions. Intermediate transaction results must be hidden from other concurrently executed transactions. That is, for every pair of transactions T i and T j, it appears to T i that either T j, finished execution before T i started, or T j started execution after T i finished. Durability( 持 久 性 ) After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures.
11 Example of Fund Transfer Transaction to transfer $50 from account A to account B Read & Write 1. read(a) 2. A := A write(a) 4. read(b) 5. B := B write(b)
12 Properties requirement (A) Atomicity requirement if the transaction fails after step 3 and before step 6, the system should ensure that its updates are not reflected in the database, else an inconsistency will result. transaction- management component (C) Consistency requirement the sum of A and B is unchanged by the execution of the transaction.
13 Properties requirement (I) Isolation requirement if between steps 3 and 6, another transaction 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. concurrency-control component (D) Durability requirement once the user has been notified that the transaction has completed (i.e., the transfer of the $50 has taken place), the updates to the database by the transaction must persist despite failures. recovery- management component
14 Transaction State Active ( 活 动 ) the initial state; the transaction stays in this state while it is executing Partially committed ( 部 分 提 交 ) after the final statement has been executed. Failed ( 失 败 ) after the discovery that normal execution can t proceed. Aborted ( 中 止 ) after the transaction has been rolled back and the database restored to its state prior to the start of the transaction. Two options after it has been aborted restart the transaction; only if no internal logical error kill the transaction Committed ( 提 交 ) after successful completion Compensating translation
15 Transaction State (Cont.)
16 Implementation of Atomicity and Durability The recovery-management component of a database system implements the support for atomicity and durability. The shadow-database scheme: assume that only one transaction is active at a time. a pointer called db_pointer always points to the current consistent copy of the database. all updates are made on a shadow copy of the database, and db_pointer is made to point to the updated shadow copy only after the transaction reaches partial commit and all updated pages have been flushed to disk. in case transaction fails, old consistent copy pointed to by db_pointer can be used, and the shadow copy can be deleted.
17 Implementation of Atomicity and Durability (Cont.) The shadow-database scheme: Assumes disks do not fail Useful for text editors, but extremely inefficient for large databases (why?) Does not handle concurrent transactions Will study better schemes later
18 Concurrent Executions Multiple transactions are allowed to run concurrently in the system. Advantages: increased processor and disk utilization, leading to better transaction throughput: one transaction can be using the CPU while another is reading from or writing to the disk, resulting in higher utilization reduced average response time for transactions: short transactions need not wait behind long ones. Concurrency control schemes ( 并 发 控 制 机 制 ) mechanisms to achieve isolation; that is, to control the interaction among the concurrent transactions in order to prevent them from destroying the consistency of the database will study next
19 Schedules ( 调 度 ) Schedule a sequences of instructions that specify the chronological order in which instructions of concurrent transactions are executed a schedule for a set of transactions must consist of all instructions of those transactions must preserve the order in which the instructions appear in each individual transaction. A transaction that successfully completes its execution will have a commit instructions as the last statement (will be omitted if it is obvious) A transaction that fails to successfully complete its execution will have an abort instructions as the last statement (will be omitted if it is obvious)
20 Schedule 1 Let T 1 transfer $50 from A to B, and T 2 transfer 10% of the balance from A to B. A serial schedule in which T 1 is followed by T 2 : If A=1000, B=2000; After the transaction? A=855;B=2145
21 Schedule 2 A serial schedule where T 2 is followed by T 1 If A=1000, B=2000; After the transaction? A=850;B=2150
22 Schedule 3 Let T 1 and T 2 be the transactions defined previously. The following schedule is not a serial schedule, but it is equivalent to Schedule 1. If A=1000, B=2000; After the transaction? A=855;B=2145 In Schedules 1, 2 and 3, the sum A + B is preserved.
23 Schedule 4 The following concurrent schedule does not preserve the value of (A + B). If A=1000, B=2000; After the transaction? A=900;B=2150 A=1000 A=950 A=1000 temp=100 A=900 A=900 B=2000 A=900 B=2000 B=2050 B=2050 B=2150 B=2150
24 Serializability Basic Assumption Each transaction preserves database consistency. Thus serial execution of a set of transactions preserves database consistency. A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. Different forms of schedule equivalence give rise to the notions of: conflict serializability( 冲 突 可 串 行 化 ) view serializability ( 视 图 可 串 行 化 ) We ignore operations other than read and write instructions we assume that transactions may perform arbitrary computations on data in local buffers between reads and writes Our simplified schedules consist of only read and write instructions
25 Conflicting Instructions Instructions l i and l j of transactions T i and T j respectively, conflict if and only if there exists some item Q accessed by both l i and l j, and at least one of these instructions wrote Q. 1. l i = read(q), l j = read(q). l i and l j don t conflict. 2. l i = read(q), l j = write(q). They conflict. 3. l i = write(q), l j = read(q). They conflict 4. l i = write(q), l j = write(q). They conflict Intuitively, a conflict between l i and l j forces a (logical) temporal order between them. If l i and l j are consecutive in a schedule and they do not conflict, their results would remain the same even if they had been interchanged in the schedule.
26 Example of Conflict instructions Read(B) and write(a) are accessing different items They are not conflict. Interexchange their order doesn t affect schedule output. read(b) write(b) read(a) write(a) Schedule 3 Schedule 5
27 Conflict Serializability If a schedule S can be transformed into a schedule S by a series of swaps of nonconflicting instructions, we say that S and S are conflict equivalent( 冲 突 等 价 ). We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule
28 Example of Conflict Serializability Schedule 3 can be transformed into Schedule 6, a serial schedule where T 2 follows T 1, by series of swaps of non-conflicting instructions. Therefore Schedule 3 is conflict serializable. Schedule 3 Schedule 6
29 Conflict Serializability (Cont.) Example of a schedule that is not conflict serializable: We are unable to swap instructions in the above schedule to obtain either the serial schedule < T 3, T 4 >, or the serial schedule < T 4, T 3 >.
30 Other Notions of Serializability The schedule below produces same outcome as the serial schedule < T 1, T 5 >, yet is not conflict equivalent or view equivalent to it. Determining such equivalence requires analysis of operations other than read and write. View equivalent and view serializable
31 Testing for Serializability Precedence graph( 优 先 图 ) a direct graph where the vertices are the transactions and lines are conflicts with arrow indicating the required order of instructions Consider some schedule of a set of transactions T 1, T 2,..., T n, we draw an arc from T i to T j if the two transaction conflict, and T i accessed the data item on which the conflict arose earlier.
32 Examples x Schedule 1 Schedule 2 Schedule 4
33 Example Schedule (Schedule A) + Precedence Graph T 1 T 2 T 3 T 4 T 5 read(x) read(y) read(z) read(v) read(w) read(w) read(y) write(y) write(z) read(u) read(y) write(y) read(z) write(z) read(u) write(u) T 1 T 2 T 3 T 4
34 Test for Conflict Serializability A schedule is conflict serializable if and only if its precedence graph is acyclic. Cycle-detection algorithms exist which take order n 2 time, where n is the number of vertices in the graph. (Better algorithms take order n + e where e is the number of edges.) If precedence graph is acyclic, the serializability order can be obtained by a topological sorting of the graph. This is a linear order consistent with the partial order of the graph. For example, a serializability order for Schedule A would be T 5 T 1 T 3 T 2 T 4 Another one?
35 Recoverable Schedules Need to address the effect of transaction failures on concurrently running transactions. Recoverable schedule If a transaction T j reads a data item previously written by a transaction T i, then the commit operation of T i appears before the commit operation of T j. The following schedule (Schedule 11) is not recoverable if T 9 commits immediately after the read If T 8 should abort, T 9 would have read (and possibly shown to the user) an inconsistent database state. Hence, database must ensure that schedules are recoverable.
36 Cascading Rollbacks( 级 联 回 滚 ) Cascading rollback A single transaction failure leads to a series of transaction rollbacks. Consider the following schedule where none of the transactions has yet committed (so the schedule is recoverable) If T 10 fails, T 11 and T 12 must also be rolled back. Can lead to the undoing of a significant amount of work
37 Cascadeless Schedules( 无 级 联 调 度 ) Cascadeless schedules Cascading rollbacks cannot occur for each pair of transactions T i and T j if T j reads a data item previously written by T i, the commit operation of T i appears before the read operation of T j. Every cascadeless schedule is also recoverable It is desirable to restrict the schedules to be cascadeless
38 Concurrency Control A database must provide a mechanism that will ensure that all possible schedules are either conflict or view serializable recoverable and preferably cascadeless A policy in which only one transaction can execute at a time generates serial schedules, but provides a poor degree of concurrency Are serial schedules recoverable/cascadeless? Testing a schedule for serializability after it has executed is a little too late! Goal to develop concurrency control protocols that will assure serializability.
39 Concurrency Control vs. Serializability Tests Concurrency-control protocols allow concurrent schedules, but ensure that the schedules are conflict/view serializable, and are recoverable and cascadeless. Concurrency control protocols generally do not examine the precedence graph as it is being created Instead a protocol imposes a discipline that avoids unseralizable schedules. We study such protocols next. Different concurrency control protocols provide different tradeoffs between the amount of concurrency they allow and the amount of overhead that they incur. Serializability Tests help us understand why a concurrency control protocol is correct.
40 Weak Levels of Consistency Some applications are willing to live with weak levels of consistency, allowing schedules that are not serializable E.g. a read-only transaction that wants to get an approximate total balance of all accounts E.g. database statistics computed for query optimization can be approximate Such transactions need not be serializable with respect to other transactions Tradeoff accuracy for performance