1 of 33

CSE 344: Section 7

Transactions

Feb 20th, 2025

2 of 33

Announcements

  • Homework 5
    • Due yesterday, late deadline EOD today!
  • Homework 6
    • Due at 11:59 pm on Wednesday, Feb 26th
    • Focus on transactions
  • Questions?

3 of 33

Txns and Serializability

4 of 33

Why Txns or Locks?

Locking is for database internal implementation!

  • Provides ACID guarantees
    • Atomicity - all or nothing (the entire transaction takes place at once or doesn’t happen at all)
    • Consistency - preserve database integrity (the database must be consistent/valid before and after the transaction)
    • Isolation - execute as if they were run alone (multiple transactions occur independently without interference)
    • Durability - results aren’t lost by a failure (the changes of a successful transaction occurs even if the system failure occurs)
  • Pessimistic concurrency control

5 of 33

Atomicity

What? “Atom” - a transaction is indivisible; data operations inside the transaction work in an all or nothing fashion

How? Transaction commit and rollback

Example: Transfer of funds from one account to another involves an atomic transaction that includes operations of debit and credit

6 of 33

Consistency

What? Only data operations that comply with database validity constraints are allowed.

Once a transaction is completed, it must not leave the database incomplete or inconsistent.

How? Enforcement of consistency rules upon data entry/updation

Example: A database having both first and last name fields will not accept if you enter only one of those - must enter both for the transaction to be complete

7 of 33

Isolation

What? The appearance of a transaction happening by itself irrespective of what other transactions are happening concurrently

How? User views the database that was present right before the view request is made and any further transactions that are in process after it are ignored

Example: A teller looking up a balance must be isolated from a concurrent transaction involving a withdrawal from the same account

8 of 33

Durability

What? Once a transaction is complete the information as changed will survive failures of any kind

How? Creation of data “mirrors”

Example: Trying to make an update that leads to a system failure should not tamper with any of the previous data and transactions

9 of 33

Serializabilities

Serial: A schedule where each transaction would be executed in some order. They do not allow concurrency; only one transaction executes at a time.

Serializable: A schedule equivalent to a serial schedule. We cannot tell them apart from a true serial schedule based on the database input and output states.

Conflict Serializable: A schedule that can be transformed into a serial schedule by swapping adjacent non-conflicting operations. (Easy to check!)

10 of 33

Conflict Serializable

“Conflict serializable” is a stronger constraint than “serializable”

I.e. any schedule that is conflict serializable must be serializable.

Serializable

Conflict

Serializable

Easy to check

Not easy to check

11 of 33

Conflicts

READ-WRITE

WRITE-READ

WRITE-WRITE

Inter-transactional

  • Represented with a conflict graph
  • Conflicts across separate transactions

12 of 33

Conflict Serializability

Checking for conflict serializability -> precedence graph and cycle checking

13 of 33

Conflict Serializability

Checking for conflict serializability -> precedence graph and cycle checking

14 of 33

Serializability Ex.

S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)

S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)

Are these serializable? Conflict serializable?

15 of 33

Serializability Ex.

S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)

S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)

Conflict Serializable

Serial order: T1, T2, T3

16 of 33

Serializability Ex.

S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)

S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)

Conflict Serializable

Potentially Serializable (but not conflict serializable)

Serial order: T1, T2, T3

Serial order: T1, T2, T3

17 of 33

Worksheet

18 of 33

WS Problem 2

T1

T2

T3

R(C)

W(D)

R(B)

W(C)

R(A)

W(B)

We can start with the serial order the problem requires. This means all transactions are completed sequentially. As long as we do valid swaps, we should have an equivalent schedule at the end!

19 of 33

WS Problem 2

T1

T2

T3

R(C)

W(D)

R(B)

W(C)

R(A)

W(B)

We want to get T1 to commit before T3 starts! Let’s start with moving R1A

20 of 33

WS PROBLEM 2

T1

T2

T3

R(C)

W(D)

R(B)

W(C)

R(A)

W(B)

We want to get T1 to commit before T3 starts! Let’s start with moving R1A

21 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(C)

W(D)

R(B)

W(C)

W(B)

Looks like T1 is the only transaction that has any action on A, so we can safely swap it to the top

22 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(C)

W(D)

R(B)

W(C)

W(B)

Now let’s see what we can do with W1B

23 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(C)

W(D)

R(B)

W(B)

W(C)

Uh oh, we can’t swap these! We need to move R2B to happen before T3 starts too.

24 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(C)

W(D)

R(B)

W(B)

W(C)

We can safely swap R2B up since T3 does not have to do with the attribute B!

25 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(B)

R(C)

W(D)

W(B)

W(C)

Now we can go back to W1B

26 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(B)

R(C)

W(D)

W(B)

W(C)

Now we can go back to W1B. We can safely swap it up!

27 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(B)

W(B)

R(C)

W(D)

W(C)

Now we can go back to W1B. We can safely swap it up!

28 of 33

WS PROBLEM 2

T1

T2

T3

R(A)

R(B)

W(B)

R(C)

W(D)

W(C)

Transaction 1 commits before transaction 3 starts (as specified in the problem). We’re done!

29 of 33

WS PROBLEM 2: Solution 1

T1

T2

T3

R(A)

R(B)

W(B)

R(C)

W(D)

W(C)

R1(A), R2(B), W1(B), Co1, R3(C), W3(D), Co3, W2(C), Co2

30 of 33

Some Alternate Solutions

31 of 33

WS PROBLEM 2: Solution 2

T1

T2

T3

R(B)

R(A)

W(B)

R(C)

W(D)

W(C)

R2(B), R1(A), W1(B), Co1, R3(C), W3(D), Co3, W2(C), Co2

32 of 33

WS PROBLEM 2: Solution 3

T1

T2

T3

R(A)

R(B)

W(B)

R(C)

W(C)

W(D)

R1(A), R2(B), W1(B), Co1, R3(C), W2(C), Co2, W3(D), Co3

33 of 33

WS PROBLEM 2: Solution 4

T1

T2

T3

R(B)

R(A)

W(B)

R(C)

W(C)

W(D)

R2(B), R1(A), W1(B), Co1, R3(C), W2(C), Co2, W3(D), Co3