1 / 17

TRANSACTIONS

TRANSACTIONS. Definition. O ne or more SQL statements that operate as a single unit . Each statement in the unit is completely interdependent. If one statement fails, they all must fail, and be rolled back. EXAMPLE.

harvey
Download Presentation

TRANSACTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TRANSACTIONS

  2. Definition • One or more SQL statements that operate as a single unit. • Each statement in the unit is completely interdependent. • If one statement fails, they all must fail, and be rolled back.

  3. EXAMPLE • When you scan your groceries at a self-serve counter, changes will need to occur in: • inventory amounts, ordering data, subtotals and taxes. • But when you go to pay and discover that the $50.00 bill you insert is a forgery, none of the calculations can be made permanent because you have not been able to pay for them. • You would press a cancel key and the entire transaction would be nullified. • The inventory information and other data would need to go back to it's original status.

  4. ASLO • If any of the above processes failed (interdependency) • a product not scanning properly • a power outage • you would not be able to complete the transaction and you would expect everything to return to normal.

  5. Transactions on ACID • ACID (atomicity, consistency, isolation, and durability) is an acronym and mnemonic device that represents the four attributes that a transactional RDBMS must have.

  6. Atomicity • When two or more tasks are required to perform a transaction, all individual tasks must complete in order for the transaction to be successful. If one or more tasks remain incomplete, the entire transaction and all of it's already completed tasks must be undone. An incomplete transaction should not leave any residue. • Example: When a widget is purchased from a store, the inventory should get modified, financial balances should be adjusted and tax balances should get adjusted.

  7. Consistency • A transaction does one of the following: • a) It creates a new state in the data when the transaction is successful • Example: The buyer pays $200.00 plus taxes for the widget and the inventory gets adjusted, less one item, the balances increase by $200.00 and the tax balances increase accordingly.

  8. Consistency • b) It returns to it's previous state when a transaction fails (in database terms, it is rolled back). • Example: If the buyer pays $200.00 for the widget and the balances adjust to a different amount, or the taxes are not added properly, the transaction should be rolled back to the original balances prior to the transaction beginning.

  9. Isolation • A transaction is completely isolated from other transactions on a system and cannot be visibly observed until it has completed. • Example: One cannot see that the inventory for widgets has been reduced until the financial records have also been adjusted. (involves a mechanism called locking)

  10. Durability • Transactions that have completed (been committed) are guaranteed to persist even if a system crash of any type occurs subsequent to the completion. • Example (how MySQL promises durability): • Two thousand transactions have occurred since the last backup. • Then the building blacks-out and the reserve generators do not kick in. • Once the power is back up, the most recent backup can be installed in combination with the transaction logs (2,000 entries) to recreate the exact database state that existed prior to the blackout. • In MySQL, all InnoDB transactions are automatically committed while MyISAM tables require a FLUSH TABLES command to commit any transactions that have occurred since the last FLUSH TABLES command.

  11. Locking • MySQL includes support for various types of locking that are dependent on the table types being used. The three major locking types are: • Table locks • Page locks • Row locks

  12. Table Locks • When a table is locked, no one else can modify or view any part of the table until the lock is released. This is the only type of lock that the MyISAM engine supports. • MyISAM – type of storage engine used in MYSQL (old)

  13. Page Locks • When one or more rows are being used, they are collectively known as a page. A page lock forbids access to these rows while still allowing access to other rows from tables that are not a part of that page. The BDB engine supports page locks. • Berkeley DB transactional storage engine. This storage engine typically is called BDB for short. BDB tables may have a greater chance of surviving crashes and are also capable of COMMIT and ROLLBACK operations on transactions.

  14. Row Locks • The most detailed lock is a row lock. Each individual row being used in a transaction has it's own lock. There is a forfeit in performance for this high level of security and functionality. The InnoDB engine uses row locks. • InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data

  15. SQL Transactions (using InnoDB) mysql> START TRANSACTION; mysql> INSERT INTO table-name VALUES (blah, "blah", 42); mysql> INSERT INTO table-name VALUES (blah, "blah", 43); mysql> INSERT INTO table-name VALUES (blah, "blah", 44); mysql> COMMIT; • Normally, InnoDB auto-commits each individual INSERT statement but when contained by a START TRANSACTION and a COMMIT statement, none of the statements are committed until the COMMIT statement is executed.

  16. ROLLBACK mysql> START TRANSACTION; mysql> INSERT INTO table-name VALUES (blah, "blah", 42); mysql> INSERT INTO table-name VALUES (blah, "blah", 43); mysql> ROLLBACK; mysql> INSERT INTO table-name VALUES (blah, "blah", 44); mysql> COMMIT; • This example would only commit the "44" record to the table. The 42 and 43 records would not be added.

More Related