120 likes | 151 Views
Transactions - generalities. Transactions - example. Parts P_id, P_name, Colour, Weight, Total_qty Contracted S_id, P_id, Qty Update add a new contract between ‘S4’ for 200 pieces of ‘P1’. Example extension. Transaction. logical unit of work sequence of database operations
E N D
Transactions - example • Parts • P_id, P_name, Colour, Weight, Total_qty • Contracted • S_id, P_id, Qty • Update • add a new contract between ‘S4’ for 200 pieces of ‘P1’
Transaction • logical unit of work • sequence of database operations • transforms a consistent state of a db into another consistent state • between operations the db can be inconsistent
Pseudo-code BEGIN TRANSACTION; INSERT (‘S4’, ‘P1’, 200) INTO Contracted; IF any error occurred THEN GO TO undo; UPDATE Parts WHERE P_id =‘P1’ SET Total_qty = Total_qty + 200; IF any error occurred THEN GO TO undo; COMMIT TRANSACTION; GO TO finish; undo: ROLLBACK TRANSACTION; finish: RETURN;
SQL support • COMMIT and ROLLBACK • No BEGIN TRANSACTION (in SQL2 and Oracle) • all data definition and data manipulation statements are transaction initiating • PostgreSQL provides • BEGIN [TRANSACTION]
Transaction processing • do not allow for • one operation to be performed and the other ones not • the system should guarantee • that all constituent operations will be executed • not possible! hence the following principle: • principle of transaction processing support • if some operations are executed and then a failure occurs (before the planned termination) then those operations will be undone
Transaction manager • COMMIT TRANSACTION • a logical unit of work was successfully completed • all the updates can be made permanent • ROLLBACK TRANSACTION • unsuccessful end of transaction • all the attempted updates must be rolled back
COMMIT and ROLLBACK points BEGIN TRANSACTION COMMIT COMMIT time transaction time idle same point as far as the DB state is concerned BEGIN TRANSACTION COMMIT ROLLBACK time transaction time idle same state of DB
At the COMMIT point • all updates, since the previous commit, are made permanent (will not be undone) • all database positioning and all tuple locks are lost
ACID properties of transactions • Atomicity • all or nothing • Consistency • preserve database consistency • Isolation • transactions are isolated from one another • Durability • committed transaction updates are performed
Pointers ahead • recovery • concurrency