Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transactions Dr. Charles Severance www.php-intro.com.

Similar presentations


Presentation on theme: "Transactions Dr. Charles Severance www.php-intro.com."— Presentation transcript:

1 Transactions Dr. Charles Severance

2 Transactions Transaction processing is designed to maintain a system’s integrity (typically a database or some modern filesystems) in a known, consistent state, by ensuring that interdependent operations on the system are either all completed successfully or all canceled successfully. Text

3 Transactions and MySQL
CREATE TABLE webauto_rps ( rps_guid VARCHAR(64) NOT NULL, ... ) ENGINE = InnoDB DEFAULT CHARSET=utf8;

4 Bank Balance: 200 ATM ATM

5 Bank Balance: 200 ATM ATM Balance: 200

6 Bank Balance: 200 ATM ATM Balance: 200 Balance: 200

7 Bank Balance: 200 ATM ATM Balance: 200 Balance: 200

8 Bank Balance: 100 ATM ATM Balance: 100 Balance: 200 $100

9 Bank Balance: 100 ATM ATM Balance: 100 Balance: 100 $100 $100

10 Bank Balance: 100 ATM ATM Balance: 100 Balance: 100 $100 $100 Awesome!

11 Transactions Allows a sequence of steps to work as a unit or not work at all Handles the fact that some things take time Locks data on read so that it cannot be read by another transaction until the current transaction completes

12 Transactions in MySQL Tables where transactions will be used require the use of the InnoDB engine. BEGIN - Starts a transaction SELECT .... FOR UPDATE – Reads and locks row (or rows) COMMIT - Completes a transaction ROLLBACK - Gives up on a transaction

13 CREATE TABLE accounts (
number INT, balance FLOAT, PRIMARY KEY(number) ) ENGINE InnoDB; INSERT INTO accounts(number, balance) VALUES(12345, 1025); INSERT INTO accounts(number, balance) VALUES(67890, 140); mysql> select * from accounts; | number | balance | | | | | | | 2 rows in set (0.02 sec) mysql>

14 BEGIN; SELECT balance FROM accounts WHERE number=12345 FOR UPDATE; UPDATE accounts SET balance=balance+25 WHERE number=12345; COMMIT; mysql> select * from accounts; | number | balance | | | | | | | 2 rows in set (0.00 sec) mysql>

15 mysql> BEGIN; mysql> UPDATE accounts SET balance=balance-250 WHERE number=12345; mysql> UPDATE accounts SET balance=balance+250 WHERE number=67890; mysql> SELECT * FROM accounts; | number | balance | | | | | | | mysql> ROLLBACK; Query OK, 0 rows affected (0.34 sec) | | | | | |

16 Transaction started but not committed.
SELECT waits

17 Transaction started but not committed.
SELECT timeout

18 Transaction started but not committed.
UPDATE waits

19 Transaction committed.
UPDATE completes instantly

20 Balances properly reflect both reductions

21 Transaction Deadlock If process A Locks X, and process B locks Y, and then process A waits for Y and process B waits for Y - they wait forever. This is called a “deadlock”. Deadlocks are avoided by making sure all transactions take locks in the same order - this way one will “win” at the beginning.

22 Start two transactions and lock two accounts

23 Attempt to lock an account but need to wait....

24 Attempt to lock an already locked account - deadlock

25 First transaction aborted so second select immediately gets lock

26

27 tsugi/samples/rps/play.php $stmt = $pdo->prepare("SELECT rps_guid, play1, play2, displayname FROM {$p}rps LEFT JOIN {$p}user ON {$p}rps.user1_id = {$p}user.user_id WHERE play2 IS NULL ORDER BY started_at ASC LIMIT 1 FOR UPDATE"); $stmt1 = $pdo->prepare("UPDATE {$p}rps SET user2_id = :U2ID, play2 = :PLAY WHERE rps_guid = :GUID"); $pdo->beginTransaction(); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ( $row == FALSE ) { $pdo->rollBack(); } else { $stmt1->execute(array(":U2ID" => $_SESSION['id'], ":PLAY" => $play, ":GUID" => $row['rps_guid'])); $pdo->commit(); }

28 Transaction Trade-offs
Transactions are essential in situations where data is read, updated, and written in a way that timing is critical. But transactions incur a cost because they lock areas of the database and pause other operations waiting for the transaction to finish. There is both a resource and performance cost if transactions are used excessively.

29 Summary M ySQL transactions Locks and deadlocks
Transaction trade-offs

30 Acknowledgements / Contributions
Continue new Contributors and Translators here These slides are Copyright Charles R. Severance ( as part of and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates


Download ppt "Transactions Dr. Charles Severance www.php-intro.com."

Similar presentations


Ads by Google