transaction processing. objectives after completing this lesson, you should be able to do the...

19
Transaction Processing

Upload: spencer-kennedy

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Transaction Processing

Page 2: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Objectives

After completing this lesson, you should be able to do the following:– Define transactions effectively for an

application

Page 3: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Transactions

– A transaction consists of a collection of DML statements that form a logical unit of work for the application (i.e. all operations must be completed or none completed so data remains in a consistent state for application)

– Example: in processing a transfer of money between bank accounts both the withdrawal of money from one account and the deposit of money into another account must be performed; cannot have first action recorded in database without the second action

Page 4: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Database Transactions May be implicit or explicit Explicit:

– consist of a set of DML (INSERT UPDATE, DELETE) statements that make one consistent change to the data

– Controlled by using the COMMIT, SAVEPOINT, and ROLLBACK statements

Implicit: automatically performed by DBMS:– A DDL (CREATE, ALTER, DROP)

statement– A DCL (GRANT, REVOKE) statement

Page 5: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Database Transactions– Transaction begins when the first executable

SQL statement is executed– Transaction ends with one of the following

events:• COMMIT or ROLLBACK issued• DDL or DCL statement executes

(automatic commit)• User exits• System crashes

– After one transaction ends a new transaction starts automatically with the next SQL statement

Page 6: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Advantages of using Transactions

– Ensures data consistency

– Groups logically related operations into one unit

– Allows preview of data changes before making changes permanent

Page 7: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

DELETEDELETE

Controlling Transactions

Transaction

Savepoint ASavepoint A

ROLLBACK to Savepoint BROLLBACK to Savepoint B

DELETEDELETE

Savepoint BSavepoint BCOMMITCOMMIT

INSERTINSERTUPDATEUPDATE

ROLLBACK to Savepoint AROLLBACK to Savepoint A

INSERTINSERTUPDATEUPDATEINSERTINSERT

ROLLBACKROLLBACK

INSERTINSERT

Control transactions by using COMMIT, SAVEPOINT, and ROLLBACK statements (note: SAVEPOINT is not standard SQL)

Control transactions by using COMMIT, SAVEPOINT, and ROLLBACK statements (note: SAVEPOINT is not standard SQL)

Page 8: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

– Automatic commit occurs under the following circumstances:• DDL statement is issued• DCL statement is issued• Normal exit from SQL*Plus, without

explicitly issuing COMMIT or ROLLBACK

– Automatic rollback occurs under an abnormal termination of SQL*Plus or a system failure.

Implicit Transaction Processing

Page 9: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

State of Data Before COMMIT or ROLLBACK

– State of data at end of last explicit or implicit transaction can be recovered.

– Current user (i.e. issued changes to data) can review the current state of data changed by DML operations by using the SELECT statement.

– Other users cannot see results of DML statements by the current user

– Changed rows are locked; other users cannot change the data within the affected rows

Page 10: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

State of Data After COMMIT– Data changes made permanent in database

– Previous state of data is permanently lost

– All users can view the results

– Locks on the affected rows are released; those rows are available for other users to manipulate

– All savepoints are erased

Page 11: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Committing Data

UPDATE employees SET department_id = 90 WHERE emp_no = 103;1 row updated.1 row updated.

UPDATE employees SET department_id = 90 WHERE emp_no = 103;1 row updated.1 row updated.

Make changes to data using DML:

COMMIT;Commit complete.Commit complete.

Commit these changes (i.e. make changes to data permanent):

Page 12: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Discards all pending changes by using the ROLLBACK statement:– State of data at time of last commit or

implicit transaction is restored (i.e current data changes undone)

– Locks on affected rows released

DELETE FROMemployees;20 rows deleted.20 rows deleted.

ROLLBACK;Rollback complete.Rollback complete.

State of Data After ROLLBACK

- delete undone; employees table still contains 20 rows of data- delete undone; employees table still contains 20 rows of data

Page 13: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Rolling Back Changes to a Marker– Create a marker in a current transaction

by using the SAVEPOINT statement.– Roll back to that marker by using the

ROLLBACK TO SAVEPOINT statement.

SQL> UPDATE...SQL> SAVEPOINT update_done;Savepoint created.Savepoint created.SQL> INSERT...SQL> ROLLBACK TO update_done;Rollback complete.Rollback complete.

Page 14: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Statement-Level Rollback

– If a single DML statement fails during execution, only that statement is rolled back.

– The Oracle Server implements an implicit savepoint.

– All other changes are retained.– The user should terminate transactions

explicitly by executing a COMMIT or ROLLBACK statement.

Page 15: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Read Consistency

– Read consistency guarantees a consistent view of the data at all times.

– Changes made by one user do not conflict with changes made by another user.

– Read consistency ensures that on the same data:

• Readers do not wait for writers• Writers do not wait for readers

Page 16: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Implementation of Read Consistency

UPDATE employeesUPDATE employeesSET sal = 2000SET sal = 2000WHERE ename = WHERE ename = 'SCOTT';'SCOTT';

DataDatablocksblocks

RollbackRollbacksegmentssegments

changedchangedand and unchanged unchanged datadata

before before changechange“old” data“old” data

User AUser A

User BUser B

ReadReadconsistentconsistentimageimage

SELECT *SELECT *FROM employees;FROM employees;

Page 17: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Locking Oracle locks:

– Prevent destructive interaction between concurrent transactions

– Require no user action– Automatically use the lowest level of

restrictiveness– Are held for the duration of the

transaction– Have two basic modes:

• Exclusive• Share

Page 18: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Summary

Description

Makes all pending changes permanent

Allows a rollback to the savepoint marker

Discards all pending data changes

Statement

COMMIT

SAVEPOINT

ROLLBACK

Page 19: Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application

Lab Overview

– Inserting rows into the tables– Updating and deleting rows in the table– Controlling transactions