Sobes.tech
Middle+

Ποιες ανωμαλίες μπορούν να προκύψουν κατά την εκτέλεση παράλληλων συναλλαγών;

sobes.tech AI

Απάντηση από AI

When transactions are executed in parallel, the following anomalies can occur:

  1. Lost Update: One transaction overwrites the changes made by another transaction before they are committed.

    Time Transaction 1 Transaction 2
    t1 Reads the Balance value (e.g., 100)
    t2 Reads the Balance value (e.g., 100)
    t3 Increases Balance by 10 (Balance = 110)
    t4 Increases Balance by 20 (Balance = 120)
    t5 Commits the changes (Balance becomes 110)
    t6 Commits the changes (Balance becomes 120)

    Result: Despite two increments, the final Balance value is 120, not 130. The increment from Transaction 1 is lost.

  2. Dirty Read: One transaction reads uncommitted changes from another transaction. If the transaction that made the changes rolls back, the read data becomes invalid.

    Time Transaction 1 Transaction 2
    t1 Begins changing Balance (100 -> 110)
    t2 Reads Balance (gets 110)
    t3 Rolls back the change (Balance reverts to 100)
    t4 Uses Balance = 110 in calculations, although the actual value is 100

    Result: Transaction 2 bases its actions on data that ultimately was not committed.

  3. Non-Repeatable Read: During a transaction, reading the same row multiple times yields different values because another transaction has committed changes to that row between reads.

    Time Transaction 1 Transaction 2
    t1 Reads Balance (gets 100)
    t2 Changes Balance to 110 and commits
    t3 Reads Balance again (gets 110 instead of 100)

    Result: The Balance has changed within the same transaction, which can lead to incorrect results.

  4. Phantom Read: Similar to non-repeatable read, but involves the addition or deletion of rows that affect the results of a query with a predicate.

    Time Transaction 1 Transaction 2
    t1 Executes SELECT COUNT(*) FROM Orders WHERE Status = 'Pending' (gets 5)
    t2 Adds a new order with status 'Pending' and commits
    t3 Executes SELECT COUNT(*) FROM Orders WHERE Status = 'Pending' again (gets 6)

    Result: Repeated execution of the query shows "phantom" rows that were not present during the first read.

These anomalies are prevented by using different transaction isolation levels.