comp 5138 relational database management systems semester 2, 2007 lecture 8a transaction concept

14
COMP 5138 COMP 5138 Relational Database Relational Database Management Systems Management Systems Semester 2, 2007 Semester 2, 2007 Lecture 8A Lecture 8A Transaction Concept Transaction Concept

Post on 22-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

COMP 5138COMP 5138

Relational Database Relational Database

Management Systems Management Systems

Semester 2, 2007Semester 2, 2007

Lecture 8ALecture 8A

Transaction ConceptTransaction Concept

Page 2: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

2222

L11L11TransactionTransaction

ManagementManagement Definition

A transaction is a collection of one or more operations on one or more databases, which reflects a single real-world transition

In the real world, this happened (completely, all at once) or it didn’t happen at all

Commerce examples Transfer money between accountsPurchase a group of products, and mark order as filled

Student record systemRegister for a class (either waitlist or allocated)

Many DB-based computer systems are mainly used for processing transactions

Page 3: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

3333

L11L11TransactionTransaction

ManagementManagement Coding a transaction

Typically a computer-based system doing OLTP has a collection of parameterized application programsEach program is written in a high-level language, which calls DBMS to perform individual SQL statements

Either through embedded SQL converted by preprocessorOr through Call Level Interface where application constructs appropriate string and passes it to DBMSCan use program variables in queries

A single SQL statement can’t do it all!Need to update several tablesNeed control flow logic

Page 4: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

4444

L11L11TransactionTransaction

ManagementManagement COMMIT

As app program is executing, it is “in a transaction”Program can execute COMMIT

SQL command to finish the transaction successfullyThe next SQL statement will automatically start a new transaction

Even working interactively, all activity on dbms is in a txn

Each interactive SQL statement is separate txn

Page 5: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

5555

L11L11TransactionTransaction

ManagementManagement ROLLBACK

If the app gets to a place where it can’t complete the transaction successfully, it can execute ROLLBACKThis causes the system to “abort” the transaction

The database returns to the state without any of the previous changes made by activity of the transaction

Rollback can be due to:Program requestClient process deathSystem-initiated (eg to deal with deadlock or crash)

Page 6: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

6666

L11L11TransactionTransaction

ManagementManagementTransaction Definition in

SQLData manipulation language must include a construct for specifying the set of actions that comprise a transaction.

In many DBMS such as Oracle, a transaction begins implicitly

Some other DBMS provide a BEGIN TRANSACTION command

A transaction in SQL ends by:Commit commits current transaction and begins a new one.Rollback causes current transaction to abort.

Page 7: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

7777

L11L11TransactionTransaction

ManagementManagement Transaction Example

Pseudocode for a product order transaction: START TRANSACTION display greeting get order request SELECT product record If product is available Then UPDATE quantityOnOrder of product record INSERT order record send message to shipping departement End if On Error: ROLLBACK COMMIT

Page 8: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

8888

L11L11TransactionTransaction

ManagementManagement Transaction ExampleTransaction in Embeded SQL 1. EXEC SQL BEGIN DECLARE SECTION2. int flight; START TRANSACTION3. char date[10]4. char seat [3] 5. int occ;6. EXEC SQL END DECLARE SECTION7. Void chooseSeat() {8. /*C code to prompt the user to enter flight, date, and seat and store these in the three variables

with those name */9. EXEC SQL Select occupied into :occ10. From Flights11. Where fltNum=:flight and fltDate=:date and fltSeat=:seat;12. If (!occ) {13. EXEC SQL Update Flights14. Set occupied = true15. Where fltNum=:flight and fltDate=:date and fltSeat=:seat; 16. /*C and SQL code to record the seat assignment and inform the user of the assignment */17. }18. Else /* C code to notify user of unavailability and ask for another seat selection */19. }

Page 9: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

9999

L11L11TransactionTransaction

ManagementManagement Atomicity

Two possible outcomes for a transactionIt commits: all the changes are madeIt aborts: no changes are made

That is, transaction’s activities are all or nothingAlso, the decision is irrevocable; so once committed, the transaction can’t revert to aborted

Changes are durable

Page 10: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

10101010

L11L11TransactionTransaction

ManagementManagement Integrity

A real world state is reflected by collections of values in the tables of the DBMSBut not every collection of values in a table makes sense in the real worldThe state of the tables is restricted by integrity constraints

Perhaps, but not always, declared explicitly in the database schemaEg foreign key constraint, primary key constraint

Page 11: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

11111111

L11L11TransactionTransaction

ManagementManagement Consistency

Each transaction can be written on the assumption that all integrity constraints hold in the data, before the transaction runsIt must make sure that its changes leave the integrity constraints still holding

However, there are allowed to be intermediate states where the constraints do not hold

A transaction that does this, is called consistentThis is an obligation on the programmer

Usually the organization has a testing/checking and sign-off mechanism before an application program is allowed to get installed in the production system

Page 12: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

12121212

L11L11TransactionTransaction

ManagementManagementSystem threats to data integrity

Need for application rollbackPrevious state must be re-installed

System crashDue to buffering, state after crash may be weird mix of pages: obsolete, over-fresh, accurate

Concurrent activityData interference; avoided by isolation

The system has mechanisms to handle these

Page 13: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

13131313

L11L11TransactionTransaction

ManagementManagement Isolation

To make precise what it means to have no data interference, we say that an execution is serializable whenThere exists some serial (ie batch, no overlap at all) execution of the same transactions which has the same final state

Hopefully, the real execution runs faster than the serial one!

NB: different serial txn orders may behave differently; we ask that some serial order produces the given state

Other serial orders may give different final states

Page 14: COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept

14141414

L11L11TransactionTransaction

ManagementManagementACID Properties

When transactions execute, users want to think of each as a single meaningful operationThe programmer and the system infrastructure together should ensure the ACID propertiesAtomic (provided by infrastructure, provided programmer puts transaction boundaries in the right place)Consistent (provided by programmer)Isolated (provided by infrastructure, provided programmer asks for it)Durable (provided by infrastructure)