Lecture07 Transactions2
Lecture07 Transactions2
Transactions
As we study before…
A DML statement is executed when you:
– Add new rows to a table
– Modify existing rows in a table
– Remove existing rows from a table
Transactions
A transaction can be seen as a collection of DML statements that form a
logical unit of work.
Problem: Interruptions
●
Two SQL statements are written to transfer
money from one bank account to another…
●
…one executes, then the server crashes
– What happens to the money?
Problem: Concurrency
●
Two users check the balance of the same bank
account…
●
…then both try to transfer money out of it
– Who gets it?
Solution: Transactions
●
A transaction is a collection of SQL statements that
must be executed as a unit
●
A database transaction consists of one of the
following:
– DML statements that constitute one consistent change to the
data.
– One DDL statement.
– One data control language (DCL) statement.
Transactions
●
The Transaction manager in the DBMS must
handle
– Interruptions: Logging and recovery, Buffers
– Concurrency: Concurrency control, Lock tables
Components of a DBMS
From Ullman/Widom
Transactions in Oracle
●
Any operation that changes the database state starts an
implicit transaction in Oracle
●
Transactions can be explicitly started with a SET
TRANSACTION statement
●
Transactions end with a COMMIT statement
●
Transaction can also be ended with ROLLBACK, usually
done by system rather than user…
Transactions: Begin and End
●
A transaction begins when the first DML SQL statement is
executed.
●
A transaction ends with one of the following events:
– A COMMIT or ROLLBACK (w/o savepoint) statement is issued.
– A DDL or DCL statement executes (automatic commit).
– The user exits from ORACLE DB utilities and tools.
●
Applications should explicitly commit or undo.
– The system crashes.
COMMIT & ROLLBACK
Advantages
With COMMIT and ROLLBACK statements, you
can:
●
Ensure data consistency.
●
Preview data changes before making changes
permanent.
●
Group logically-related operations.
Explicit Transaction Control
●
COMMIT, makes changes permanent
●
ROLLBACK, undoes changes
●
SAVEPOINT, which marks a savepoint in a
transaction
Transactions
●
A transaction is a collection of SQL statements that
must be executed as a unit
●
Transactions have ACID properties:
– Atomicity
– Consistency
– Isolation
– Durability
ACID Properties
●
A transaction should satisfy the following
properties:
– Atomicity: Executes completely or not at all
– Consistency: Satisfies all database constraints
– Isolation: Executes “separately” from others
– Durability: Once executed, results are permanent
ACID Properties
●
Another way to look at these properties:
– Atomicity: Entire transaction takes places at once or it does not
happen at all
– Consistency: State of database consistent before and after
transaction
– Isolation: Multiple transactions happen independently with no
interference
– Durability: No data lost even if the system fails
Atomicity
●
Transaction operations are kept in a local store, not applied
to the database immediately
– Transaction can see its own changes, others can’t
●
When a transaction is completed, COMMIT applies changes
to the shared database in their entirety (“executes
completely…”)
●
ROLLBACK during a transaction undoes any partial results
(“…or not at all”)
Consistency
●
Constraints can be “deferred”, so that they are
only checked when a transaction commits, not
for each individual statement
– Add DEFERRABLE INITIALLY DEFERRED to the
constraint definition
– If a constraint is violated when you COMMIT, a
ROLLBACK of the entire transaction is done!
Data is consistent!
Durability
●
After COMMIT, the changes applied to the
shared database are permanent, and cannot be
rolled back later.
Data is persistent!
Isolation
●
DBMS maintains “separation” among
transactions that access data concurrently
– Various levels of isolation
– Tradeoffs between performance and data integrity
Isolation Levels
●
SERIALIZABLE: Transactions must appear to run
serially – cannot read any changes from others
●
REPEATABLE READ: Can read committed changes that
only add data
●
READ COMMITTED: Can read all committed changes
●
READ UNCOMMITTED: Can read all changes,
committed and not committed
Transactions in Oracle
●
SET TRANSACTION statement can specify
ISOLATION LEVEL
– READ COMMITTED (default)
– SERIALIZABLE
– Oracle does not support REPEATABLE READ and READ
UNCOMMITTED
●
COMMIT or ROLLBACK ends transaction
Midterm Exam Information
●
Exam Wednesday, February 5th
●
Logistics
– In-person section, in-person in CDM 222
– 1 hour
– You may use one 8.5”x11” sheet of original, handwritten notes for the exam (both sides)
●
Everything cover in class and D2L material
●
Ullman/Widom Sections 1.1-1.3, 2.1-2.3, 4.1, 6.1-6.5, 7.1-7.3
●
Short answer questions, multiple choice, writing and/or evaluating SQL DDL/DML code and queries, etc.