mysql5.x have been released for a long time, but there are many people think that MySQL is not support transaction processing, this has to blame them is ignorant, in fact, as long as your MySQL version support BDB or InnoDB table type, then your MySQL has the ability of transaction processing. In this, but also with the InnoDB table type of the most, although later, such as Oracle acquisition InnoDB and other things that make MySQL uncomfortable, but those business struggles and technology-independent, the following InnoDB table type as an example simply talk about the transaction in MySQL.
MySQL data supports transactions, but requires that the table type must be a InnoDB storage engine
SQL statement to delete
Delete from userinfo where ~ ~ ~
Delete from Mail where ~ ~
Delete from article where~~
~~
If there is no transaction, in the process of your deletion, assuming that the error, only executed the first sentence, then the consequences are unimaginable!
But with transaction processing. If you delete an error, you can cancel the delete operation as long as you rollback ( in fact, if you do not have a commit, you do not actually perform the delete operation )
In general, transactions must be considered in the business-level (transaction-related) application!
related knowledge of the transaction:
The business should have acid characteristics. The so-called acid is Atomic (atomicity), consistent (consistency), Isolated (isolation), durable (persistent) four words written in the first letter, the following "bank transfer" as an example to explain their meaning:
atomicity : The statement that makes up the transaction forms a logical unit and cannot execute only part of it. In other words, a transaction is the smallest unit that is indivisible. For example, in a bank transfer process, you must subtract the transfer amount from one account and add it to another account, it is unreasonable to change only one account.
consistency : The database is consistent before and after the execution of the transaction. In other words, the transaction should correctly convert the System state. For example: During a bank transfer, the transfer amount is transferred from one account to another, or two accounts are not changed, and there is no other situation.
isolation : One transaction has no effect on another transaction processing. This means that no transaction is likely to see a transaction in an incomplete state. For example, in a bank transfer process, another transfer transaction can only be in the waiting state until the transfer transaction is not committed.
Persistence : The effect of transaction processing can be permanently preserved. Conversely, transactions should be able to withstand all failures, including servers, processes, communications, media failures, and so on. For example: In the process of bank transfer, the status of transfer payback can be saved.
which issues will be used for transaction processing:
This is not the case of "bank transfer", which is an example of an "online book" that we are actually more likely to encounter. Let's take a look at the background of the problem: online purchase, a book (database number 123) Only the last one, and this time, two users to this book almost at the same time the purchase request, lets us see the whole process:
Before specific analysis, let's look at the definition of the data table:
-------------------------------------------------------------------------------
CREATE TABLE book
(
book_id unsigned int (ten) not NULL auto_increment,
Book_name varchar (+) NOT NULL,
Book_price Float (5, 2) not NULL, #我假设每本书的价格不会超过999.99 USD
Book_number Int (ten) is not NULL,
Primary KEY (BOOK_ID)
)
Type = InnoDB; #engine = InnoDB is ok
-------------------------------------------------------------------------------
For user A, he moves a little bit faster than B, and the actions triggered by the purchase process are roughly the same:
-------------------------------------------------------------------------------
1. SELECT book_number from book WHERE book_id = 123;
Book_number greater than 0, confirm purchase behavior and update Book_number
2. UPDATE book SET book_number = book_number-1 WHERE book_id = 123;
Successful book purchase
-------------------------------------------------------------------------------
For User B, his movements are slightly slower than a bit, and the purchase process triggers the same action as the armor:
-------------------------------------------------------------------------------
1. SELECT book_number from book WHERE book_id = 123;
At this time, a just finished the first step of operation, not yet time to do the second step, so book_number must be greater than 0
2. UPDATE book SET book_number = book_number-1 WHERE book_id = 123;
Successful book purchase
-------------------------------------------------------------------------------
On the surface, the operation of a B was successful, they all bought a book, but only one in stock, how can they all succeed? Look at the contents of the data table Book_number, has become "1", which is certainly not allowed (in fact, declaring such a column type should be added to the unsigned property to ensure that it can not be negative, this is to illustrate the problem, so there is no such setting)
OK, the problem is clear, and then to see how to use the transaction to solve the problem, open the MySQL manual, you can see that you want to use the transaction to protect your SQL correctly executed is very simple, basically three statements: start, Commit, rollback.
-------------------------------------------------------------------------------
Start: The start transaction or BEGIN statement can start a new transaction
Commit: Commit can commit the current transaction, change becomes permanent change
Rollback: Rollback can roll back the current transaction and cancel its change
Additionally, SET autocommit = {0 | 1} can disable or enable the default autocommit mode for the current connection.
-------------------------------------------------------------------------------
Is that just a matter of wrapping up our SQL statements with a transactional statement to make sure it's right? For example, the following code:
-------------------------------------------------------------------------------
BEGIN;
SELECT book_number from book WHERE book_id = 123;
// ...
UPDATE book SET book_number = book_number-1 WHERE book_id = 123;
COMMIT;
-------------------------------------------------------------------------------
The answer is no, this still does not prevent the problem, if you want to avoid such a situation, the actual should be as follows:
-------------------------------------------------------------------------------
BEGIN;
SELECT book_number from book WHERE book_id = 123 for UPDATE;
// ...
UPDATE book SET book_number = book_number-1 WHERE book_id = 123;
COMMIT;
-------------------------------------------------------------------------------
Because a for UPDATE is added, a row lock is added to this record, and if the transaction does not end completely, the other transactions are using SELECT ... The FOR update request will wait until the last transaction finishes before it can continue, avoiding the problem, and note that if your other transactions are using a SELECT statement without a for update, this protection will not be available.
Code Demo for PHP + MySQL transaction operation:
Actual lamp application, general PHP use ADODB operation MySQL, the following gives ADODB the corresponding code for easy access to everyone:
-------------------------------------------------------------------------------
<?php
// ...
$adodb-Starttrans ();
In fact, the query called by GetOne can also be placed directly into the rowlock, here just to demonstrate the effect can be more obvious.
$adodb-Rowlock (' book ', ' book_id = 123 ');
$bookNumber = $adodb getone ("Select Book_number from book WHERE book_id = 123");
$adodb, execute ("UPDATE book SET book_number = book_number-1 WHERE book_id = 123");
$adodb-Completetrans ();
// ...
?>
-------------------------------------------------------------------------------
Where the Rowlock method is to invoke the for update to implement the row lock, you may want to "for update" directly into the $adodb->getone () called the SQL statement to implement the row lock function, yes, that is true, However, not all databases use the "for UPDATE" syntax to implement row lock functionality, such as Sybase using the "HOLDLOCK" syntax to implement row locking, so for your database abstraction layer to maintain portability, I still advise you to use Rowlock to implement the row lock function
From: http://blog.csdn.net/wang_cir/article/details/6227155
MySQL--Transaction processing