SQL Transactions and Database Isolation Levels, Slides of Introduction to Database Management Systems

The concept of sql transactions and the acid properties (atomicity, consistency, isolation, durability). It also covers different sql isolation levels (serializable, repeatable read, read committed, read uncommitted) and their implications on data consistency and concurrency.

Typology: Slides

2011/2012

Uploaded on 01/29/2012

arold
arold 🇺🇸

4.7

(24)

372 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
SQL: Transactions
CPS 116
Introduction to Database Systems
2
Announcements
Homework #2 due next Tuesday (Sept. 28)
Project milestone #1 due next Thursday
Discussion session this week (Homework #2 Q&A)
Time and place will be announced via email
3
Transactions
A transaction is a sequence of database operations
with the following properties (ACID):
Atomic: Operations of a transaction are executed all-or-
nothing, and are never left “half-done”
Consistency: Assume all database constraints are satisfied
at the start of a transaction, they should remain satisfied
at the end of the transaction
Isolation: Transactions must behave as if they were
executed in complete isolation from each other
Durability: If the DBMS crashes after a transaction
commits, all effects of the transaction must remain in the
database when DBMS comes back up
4
SQL transactions
A transaction is automatically started when a user executes
an SQL statement
Subsequent statements in the same session are executed as
part of this transaction
Statements see changes made by earlier ones in the same
transaction
Statements in other concurrently running transactio ns do not see
these changes
COMMIT command commits the transaction
Its effects are made final and visible to subseque nt transactions
ROLLBACK command aborts the transaction
Its effects are undone
5
Fine prints
Schema operations (e.g., CREATE TABLE) implicitly
commit the current transaction
Because it is often difficult to undo a schema operation
Sometime you need to turn off a feature called
AUTOCOMMIT, which automatically commits every
single statement
Example: Run DB2’s db2 command-line processor with
the option +c
More examples to come when we cover database API’s
6
Atomicity
Partial effects of a transaction must be undone when
User explicitly aborts the transaction using ROLLBACK
E.g., application asks for user confirmation in the last step and
issues COMMIT or ROLLBACK depending on the response
The DBMS crashes before a transaction commits
Partial effects of a modification statement must be
undone when any constraint is violated
However, only this statement is rolled back; the
transaction continues
How is atomicity achieved?
Logging (to support undo)
pf3

Partial preview of the text

Download SQL Transactions and Database Isolation Levels and more Slides Introduction to Database Management Systems in PDF only on Docsity!

SQL: Transactions

CPS 116

Introduction to Database Systems

Announcements

™ Homework #2 due next Tuesday (Sept. 28)

™ Project milestone #1 due next Thursday

™ Discussion session this week (Homework #2 Q&A)

ƒ Time and place will be announced via email

3

Transactions

™ A transaction is a sequence of database operations

with the following properties (ACID):

ƒ Atomic: Operations of a transaction are executed all-or-

nothing, and are never left “half-done”

ƒ Consistency: Assume all database constraints are satisfied

at the start of a transaction, they should remain satisfied

at the end of the transaction

ƒ Isolation: Transactions must behave as if they were

executed in complete isolation from each other

ƒ Durability: If the DBMS crashes after a transaction

commits, all effects of the transaction must remain in the

database when DBMS comes back up

4

SQL transactions

™ A transaction is automatically started when a user executes

an SQL statement

™ Subsequent statements in the same session are executed as

part of this transaction

ƒ Statements see changes made by earlier ones in the same transaction ƒ Statements in other concurrently running transactions do not see these changes

™ COMMIT command commits the transaction

ƒ Its effects are made final and visible to subsequent transactions

™ ROLLBACK command aborts the transaction

ƒ Its effects are undone

5

Fine prints

™ Schema operations (e.g., CREATE TABLE) implicitly

commit the current transaction

ƒ Because it is often difficult to undo a schema operation

™ Sometime you need to turn off a feature called

AUTOCOMMIT, which automatically commits every

single statement

ƒ Example: Run DB2’s db2 command-line processor with

the option +c

ƒ More examples to come when we cover database API’s

6

Atomicity

™ Partial effects of a transaction must be undone when

ƒ User explicitly aborts the transaction using ROLLBACK

  • E.g., application asks for user confirmation in the last step and issues COMMIT or ROLLBACK depending on the response

ƒ The DBMS crashes before a transaction commits

™ Partial effects of a modification statement must be

undone when any constraint is violated

ƒ However, only this statement is rolled back; the

transaction continues

™ How is atomicity achieved?

ƒ Logging (to support undo)

Durability

™ Effects of committed transactions must survive

DBMS crashes

™ How is durability achieved?

ƒ Forcing all changes to disk at the end of every

transaction?

  • Too expensive: DBMS manipulates data in memory

ƒ Logging (to support redo)

Consistency

™ Consistency of the database is guaranteed by

constraints and triggers declared in the database

and/or transactions themselves

ƒ Whenever inconsistency arises, abort the statement or

transaction, or (with deferred constraint checking or

application-enforced constraints) fix the inconsistency

within the transaction

9

Isolation

™ Transactions must appear to be executed in a serial

schedule (with no interleaving operations)

™ For performance, DBMS executes transactions using

a serializable schedule

ƒ In this schedule, operations from different transactions

can interleave and execute concurrently

ƒ But the schedule is guaranteed to produce the same

effects as a serial schedule

™ How is isolation achieved?

ƒ Locking, multi-version concurrency control, etc.

10

SQL isolation levels

™ Strongest isolation level: SERIALIZABLE

ƒ Complete isolation

ƒ SQL default

™ Weaker isolation levels: REPEATABLE READ, READ

COMMITTED, READ UNCOMMITTED

ƒ Increase performance by eliminating overhead and

allowing higher degrees of concurrency

ƒ Trade-off: sometimes you get the “wrong” answer

11

READ UNCOMMITTED

™ Can read “dirty” data

ƒ A data item is dirty if it is written by an uncommitted transaction

™ Problem: What if the transaction that wrote the dirty data

eventually aborts?

™ Example: wrong average

ƒ -- T1: -- T2:

UPDATE Student SET GPA = 3. WHERE SID = 142; SELECT AVG(GPA) FROM Student; ROLLBACK; COMMIT;

12

READ COMMITTED

™ No dirty reads, but non-repeatable reads possible

ƒ Reading the same data item twice can produce different results

™ Example: different averages

ƒ -- T1: -- T2:

SELECT AVG(GPA)

FROM Student; UPDATE Student SET GPA = 3. WHERE SID = 142; COMMIT; SELECT AVG(GPA) FROM Student; COMMIT;