11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoC H A P T E R 37• • •TransactionsThis chapter introduces <strong>MySQL</strong>’s transactional capabilities and demonstrates how transactions areexecuted both via a <strong>MySQL</strong> client and from within a <strong>PHP</strong> script. By its conclusion, you’ll possess ageneral understanding of transactions, how they’re implemented by <strong>MySQL</strong>, and how to incorporatethem into your <strong>PHP</strong> applications.What’s a Transaction?A transaction is an ordered group of database operations that are treated as a single unit. A transaction isdeemed successful if all operations in the group succeed, and is deemed unsuccessful if even a singleoperation fails. If all operations complete successfully, that transaction will be committed, and itschanges will be made available to all other database processes. If an operation fails, the transaction willbe rolled back, and the effects of all operations comprising that transaction will be annulled.Any changes effected during the course of a transaction will be made solely available to the threadowning that transaction, and will remain so until those changes are indeed committed. This preventsother threads from potentially making use of data that may soon be negated due to a rollback, whichwould result in a corruption of data integrity.Transactional capabilities are a crucial part of enterprise databases because many businessprocesses consist of multiple steps. Take, for example, a customer’s attempt to execute an onlinepurchase. At checkout time, the customer’s shopping cart will be compared against existing inventoriesto ensure availability. Next, the customer must supply their billing and shipping information, at whichpoint their credit card will be checked for the necessary available funds and then debited. Next, productinventories will be deducted accordingly, and the shipping department will be notified of the pendingorder. If any of these steps fails, then none of them should occur. Imagine the customer’s dismay tolearn that their credit card has been debited even though the product never arrived because ofinadequate inventory. Likewise, you wouldn’t want to deduct inventory or even ship the product if thecredit card is invalid or if insufficient shipping information was provided.On more technical terms, a transaction is defined by its ability to follow four te<strong>net</strong>s, embodied in theacronym ACID. These four pillars of the transactional process are defined here:• Atomicity: All steps of the transaction must be successfully completed; otherwise,none of the steps will be committed.• Consistency: All steps of the transaction must be successfully completed;otherwise, all data will revert to the state that it was in before the transactionbegan.• Isolation: The steps carried out by any as-of-yet incomplete transaction mustremain isolated from the system until the transaction has been deemed complete.709

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!