Chris Middup CM20145 CM20145 Transactions & Transactions & Serializability Serializability Lecture Plan Lecture Plan 1. Basic Concepts 2. Data, Information & Knowledge 3. Data Models (The E-R Model) 4. The Relational Algebra 5. Introduction to SQL 6. Further SQL (Joins, RA Equivalences) 7. Database Design 8. Further DB Design – Normalisation 9. Architectures and Implementations 10.Integrity and Security Lecture Plan Lecture Plan 11.Ethics and Professional Conduct 12.Legal Issues 13.Transactions 14.Recovery 15.Concurrency Control 16.Storage and File Structure 17.Indexing and Hashing 18.Query Processing & Optimisation 19.XML Databases 20.Revision A While Ago A While Ago … … Architectures and Implementations Introductions to Transactions & Storage Architecture concerns: Speed, Cost, Reliability, Maintainability. Architectural Types: Centralized, Client/Server, Parallel, Distributed Integrity and Security Domain Constraints Referential Integrity Foreign Keys, Cascading Actions Assertions Triggers Authorization Grant, Revoke, Roles, Audit Trails Now: Transactions, Concurrency & Recovery Overview Overview Transaction Concepts ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Introduction to Transactions Introduction to Transactions A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction starts with a consistent database. During transaction execution the database may be inconsistent. When the transaction is committed, the database must be consistent. Two main issues to deal with: Failures, e.g. hardware failures and system crashes. Concurrency, for simultaneous execution of multiple transactions. ©Silberschatz, Korth and Sudarshan Modifications & additions by S Bird, J Bryson The ACID Test The ACID Test To preserve 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. Durability: After a transaction completes successfully, the changes it has made to the database persist, even if the system fails. Example: A Fund Transfer Example: A Fund Transfer Transfer $50 from account A to B: 1. read(A) 2. A := A – 50 3. write(A) 4. read(B) 5. B := B + 50 6. write(B) Consistency: the sum of A and B is unchanged by the execution of the transaction. Atomicity: if the transaction fails after step 3 and before step 6, the system must ensure that no updates are reflected in the database, else an inconsistency will result. Durability: once the user notified that the transaction complete, the updates to the database by the transaction must persist despite failures. Isolation: between steps 3-6, no other transaction should access the partially updated database, or it would see an inconsistent state (A + B will be less than it should be). Transaction States Transaction States Active, the initial state; the transaction stays in this state while it is executing Partially committed, after the final statement has been executed. Committed, after successful completion. Failed, after the discovery that normal execution can no longer proceed. Aborted, after the transaction has been rolled back and the database restored to its state prior to the start of the transaction. Transaction Definition in SQL Transaction Definition in SQL Data manipulation languages must include a construct for specifying the set of actions that comprise a transaction. In SQL, a transaction begins implicitly. A transaction can be explicitly ended by: Commit work: commits current transaction and begins a new one. Rollback work: causes current transaction to abort. [...]... sufficient conditions for view serializability can still be used Overview Transaction Concepts ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Summary Transaction Concepts ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Reading & Exercises Reading Silberschatz... Testing for Serializability Precedence Graphs Conflict View Testing for Serializability Consider some schedule of a set of transactions T1, T2, , Tn Precedence graph: a directed graph where the vertices are transaction names We draw an arc from Ti to Tj if the two transactions conflict, and Ti accessed the data item before Tj We may label the arc by the item that was accessed Example: x y Schedule & Precedence... ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Schedules & Concurrency Advantages to Concurrent execution (executing transactions simultaneously): Increased processor and disk utilization; better throughput E.g one transaction uses CPU while another uses disk Reduced average response time Short transactions need not wait behind... control schemes: Mechanisms to achieve isolation Control concurrent transactions interaction to maintain database consistency Schedules: Def: Sequences that indicate 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... linear order consistent with the partial order of the graph For example, one serializability order for this graph is : T1 → T2 → T4 → T3 → T5 (note there’s another!) Example of an acyclic precedence graph Testing View Serializability The precedence graph test for conflict serializability must be modified to apply to a test for view serializability The problem of checking if a schedule is view serializable... serializable if it is equivalent to a serial schedule Different forms of equivalence lead to different kinds of serializability: conflict and view Recovery is the point of serializability Serialization makes recovery easier, but can slow down throughput Conflict Serializability Instructions li and lj of transactions Ti and Tj respectively, conflict iff there exists some item Q accessed by both li and lj, and... not conflict serializable: T3 read(Q) write(Q) T4 write(Q) Conflict Serializability (3) This concurrent schedule can be transformed into the serial one (where T2 follows T1) by a series of swaps of nonconflicting instructions Therefore it is conflict serializable View Serializability Let S and S´ be two schedules with the same set of transactions S and S´ are view equivalent if the following three conditions... Transaction Concepts ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Concurrency & Serializability Goal – to develop concurrency control protocols that will ensure serializability These protocols impose a discipline that avoids nonseralizable schedules A common concurrency control protocol uses locks While one transaction... running concurrent transactions? Recoverable schedule: if a transaction Tj reads a data item previously written by a transaction Ti , the commit operation of Ti appears before the commit operation of Tj A Database must ensure that schedules are recoverable! Serializability Basic Assumption: Each transaction, on its own, preserves database consistency That is, serial execution of transactions preserves... T2 be the transactions defined previously This schedule is not a serial schedule, but it is equivalent to the previous schedule In both this and the sequential schedule, the sum A + B is preserved Concurrency Gone Bad This concurrent schedule does not preserve the value of A + B BAD Overview Transaction Concepts ACID Possible States Schedules Serializability Conflict View Others Testing for Serializability . Chris Middup CM20145 CM20145 Transactions & Transactions & Serializability Serializability Lecture Plan Lecture. Schedules Serializability Conflict View Others Testing for Serializability Precedence Graphs Conflict View Concurrency & Concurrency & Serializability Serializability