Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems II, Hyunja Lee1 Transaction Support in SQL.

Similar presentations


Presentation on theme: "Database Management Systems II, Hyunja Lee1 Transaction Support in SQL."— Presentation transcript:

1 Database Management Systems II, Hyunja Lee1 Transaction Support in SQL

2 Database Management Systems II, Hyunja Lee2 Lifetime of a Transaction in SQL v No explicit “begin” statement. –a new transaction is implicitly began when the first SQL statement is executed. v Explicit “end” statement –Commit or Rollback ends the transaction. v Oracle 9i (using sqlplus) SQLPLUS> commit; SQLPLUS> select * from proj; // new trasaction is began SQLPLUS> insert into proj values (1, ‘Proj_X’, 500, ‘John’); SQLPLUS> commit; // the transaction is committed and ended.

3 Database Management Systems II, Hyunja Lee3 v Characteristics of a transaction is set in SET TRANSACTION statement –Must appear as the first statement in a transaction. –Can set up the following characteristics u Access mode : read only or read write u Isolation level : serializable, repeatable read, read committed, or read uncommitted. Set Transaction Statement

4 Database Management Systems II, Hyunja Lee4 v Access mode : read only or read write – Set transaction read only : set the transaction (that you now begin) as a transaction that contains only read operations (select statements). u No write operation (insert, delete, update statements) is allowed. u See changes committed before this transaction began. – Set transaction read write : set the transaction as a transaction that contains read/write operations. u default setting. Set Transaction Statement (Read only/ Read Write)

5 Database Management Systems II, Hyunja Lee5 v Wait a second…. –Why do we want to set up a transaction as read only ? What about setting up a transaction as read-write and invoking read operations only? commit; select projno from proj; select count(*) from proj; commit; insert into proj (2,’Project_Y’,100, ‘Tom’); commit; v Count(*) will give you one more number of which tuples you have seen from the previous select statements. Why? A new tuple is inserted !!!!!! Set Transaction Statement (Read only/ Read Write) Cont’d

6 Database Management Systems II, Hyunja Lee6 v Wait another second…. –Didn’t we learn that transactions should always be serialized ? In other words, isn’t is true that DBMS should support a serializable schedule automatically? commit; select projno from proj; select count(*) from proj; commit; insert into proj (2,’Project_Y’,100, ‘Tom’); commit; Set Transaction Statement (Read only/ Read Write) Cont’d T1 T2 v If DBMS supports the serializability, then the result should be the same as either T1 and T2, or T2 and T1.

7 Database Management Systems II, Hyunja Lee7 v In reality, the answer is NOT!!!! Why not? Supporting the serializability is expensive & sometimes not desirable. Set Transaction Statement (Read only/ Read Write) Cont’d v For example, Oracle DBMS does not have the serializable schedule as the default schedule. It employs read-committed isolation level as the default.

8 Database Management Systems II, Hyunja Lee8 v What if we set the transaction as read only ? commit; set transaction read only; select projno from proj; select count(*) from proj; commit; insert into proj (2,’Project_Y’,100, ‘Tom’); commit; v DBMS allows a read-only transaction to see the changes committed before the transaction began. v Oracle calls this as transaction-level read consistency (versus statement-level read consistency ). Set Transaction Statement (Read only/ Read Write) Cont’d

9 Database Management Systems II, Hyunja Lee9 Set Transaction Statement (Isolation level) v Isolation level of a transaction is set in SET TRANSACTION statement – Serializable : default in SQL standard. – Repeatable Read : prevents non-repeatable read. – Read Committed : default in Oracle DBMS. u See changes only committed by another transactions. u Prevents dirty-read anomaly. – Read Uncommitted : u See changes incurred by any (including uncommitted) transactions.

10 Database Management Systems II, Hyunja Lee10 v Serializable commit; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; select projno from proj; select count(*) from proj; commit; insert into proj (2,’Project_Y’,100, ‘Tom’); commit; Set Transaction Statement (Isolation level) Cont’d T1 T2 v If DBMS supports the serializability, then the result should be the same as either T1 and T2, or T2 and T1. v Lock the proj table in S mode so that any write operation to the proj table is not allowed. => prevent phantom.

11 Database Management Systems II, Hyunja Lee11 v Repeatable Read commit; select budget from proj where projno=2; commit; update proj set budget = 200 where projno=2; commit; Set Transaction Statement (Isolation level) Cont’d T1 T2 v Non repeatable read : the budget value of the first select is different to the budget value of the second select. v If the isolation level is set to REPEATABLE READ, then two budget values are the same. That’s why it is called repeatable read.

12 Database Management Systems II, Hyunja Lee12 v Read Committed commit; select budget from proj where projno=2; update proj set budget = budget+100 where projno=2; rollback; commit; select budget from proj where projno=2; update proj set budget = budget+100 where projno=2; Set Transaction Statement (Isolation level) Cont’d T1 T2 v Dirty read : T2 reads the budget written by T1 which has not committed.

13 Database Management Systems II, Hyunja Lee13 Set Transaction Statement (Isolation level) Cont’d Isolation levelDirty ReadNonrepeatable read Phantom SerializableNo Repeatable Read No Yes Read Committed NoYes Read Uncommitted Yes Type of anomaly stricter looser Oracle9i supports

14 Database Management Systems II, Hyunja Lee14 EXEC SQL WHENEVER SQLERROR GOTO UNDO; EXEC SQL SET TRANSACTION READ WRITE ISOLATION LEVEL SERIALIZABLE; EXEC SQL INSERT INTO EMPLOYEE (FNAME, LNAME, SSN, DNO, SALARY) VALUES (‘Robert’, ‘Smith’, ‘991001234’, 2, 35000); EXEC SQL UPDATE EMPLOYEE SET SALARY = SALARY * 1.1 WHERE DNO = 2; EXEC SQL COMMIT; GOTO THE_END; UNDO: EXEC SQL ROOBACK; THE_END: A Sample SQL Transaction v Note: This example is based on the embedded SQL syntax. This can not be run in Oracle sqlplus.

15 Database Management Systems II, Hyunja Lee15 EXEC SQL WHENEVER SQLERROR GOTO UNDO; SET TRANSACTION READ WRITE ISOLATION LEVEL SERIALIZABLE; INSERT INTO EMPLOYEE (FNAME, LNAME, SSN, DNO, SALARY) VALUES (‘Robert’, ‘Smith’, ‘991001234’, 2, 35000); UPDATE EMPLOYEE SET SALARY = SALARY * 1.1 WHERE DNO = 2; COMMIT; GOTO THE_END; UNDO: EXEC SQL ROOBACK; THE_END: A Sample SQL Transaction Cont’d v Note: You may run a part of this following block in Oracle sqlplus. But how about the control (in italic)?


Download ppt "Database Management Systems II, Hyunja Lee1 Transaction Support in SQL."

Similar presentations


Ads by Google