Php processes mysql transactions

Source: Internet
Author: User
Www.beijibear.com? Aid463 transactions must meet four conditions (ACID): atomicity (Autmic), Consistency (Consistency), Isolation (Isolation), and Durability (Durability) atomicity (Autmic ): the transaction is being executed. "Do not do it, or do it all ."

Http://www.beijibear.com /? Aid = 463 transactions must meet four conditions (ACID): atomicity (Autmic), Consistency (Consistency), Isolation (Isolation), Durability (Autmic ): the transaction is being executed. "Do not do it, or do it all ."

Http://www.beijibear.com /? Aid = 463.

TransactionsIt must meet four conditions (ACID): atomicity (Autmic), Consistency (Consistency), Isolation (Isolation), Durability (Durability)

Atomicity (Autmic ):TransactionsIn execution, "Do not do it, or do it all !", That is to say, do not allowTransactionsPartially executed. Even if the fault causesTransactionsCannot be completed. The impact on the database should also be eliminated during rollback!

Consistency ):TransactionsThe operation should make the database change from a consistent state to another consistent state! Take online shopping for example. You only need to let the goods go out of stock and let the goods enter the shopping basket of the customer.Transactions!

Isolation: if multipleTransactionsConcurrent execution should be similarTransactionsSame for independent execution!

Durability: a successful executionTransactionsThe role of the database is persistent. Even if the database fails or fails, it should be able to be restored!

MYSQLTransactionsProcessingThere are two main methods.
1. Use begin, rollback, and commit to implement
StartTransactions
RollbackTransactionsRollback
CommitTransactionsConfirm
Rollback and commit cannot be used in parallel. When you use them at the same time, only the previous one is valid, and the latter one is invalid. That is, you can execute commit or execute rollback.
2. directly use set to change the mysql automatic submission Mode
MYSQL is automatically submitted by default, that is, when you submit a QUERY, It will be executed directly! We can use
Set autocommit = 0 disable automatic submission
Set autocommit = 1 enable automatic submission
To achieveTransactionsOfProcessing.

Note that when you use set autocommit = 0, all your SQL statements will be usedTransactionsProcessingUntil you end with commit confirmation or rollback. Note that when you end thisTransactionsAt the same time, a newTransactions! In the first method, only the currentTransactions!

In MYSQL, only INNODB and BDB data tables are supported.TransactionsProcessing! Other types are not supported!
* **: The default MYSQL database engine is MyISAM, which is not supported.Transactions! If you want to make MYSQL supportTransactions, You can manually modify it:
The method is as follows: 1. Modify the c: \ appserv \ mysql \ my. ini file, find skip-InnoDB, add # in front, and save the file.
2. Enter services. msc in the running state to restart the mysql service.
3. in phpmyadmin, mysql-> show engines; (or execute mysql-> show variables like 'have _ % ';). If InnoDB is YES, InnoDB is supported.
It also indicates supportTransactionsTransaction.
4. When creating a table, you can select the InnoDB Engine for the Storage Engine. For a previously created table, you can use mysql-> alter table table_name type = InnoDB;
Or mysql-> alter table table_name engine = InnoDB; to change the data table engine to supportTransactions.

Instance 1:

 
 
  1. $ Conn = mysql_connect ('localhost', 'root', 'root') or die ("data connection Error !!! ");
  2. Mysql_select_db ('test', $ conn );
  3. Mysql_query ("set names 'gbk'"); // use GBK Chinese encoding;
  4. // StartTransactions
  5. Mysql_query ("BEGIN"); // or mysql_query ("start transaction ");
  6. $ SQL = "INSERT INTO 'user' ('id', 'username', 'sex') VALUES (NULL, 'test1', '0 ')";
  7. $ Sql2 = "insert into 'user' ('did', 'username', 'sex') VALUES (NULL, 'test1', '0 ')"; // I intentionally wrote an error.
  8. $ Res = mysql_query ($ SQL );
  9. $ Res1 = mysql_query ($ sql2 );
  10. If ($ res & $ res1 ){
  11. Mysql_query ("COMMIT ");
  12. Echo 'submission successful. ';
  13. } Else {
  14. Mysql_query ("ROLLBACK ");
  15. Echo 'data rollback. ';
  16. }
  17. Mysql_query ("END ");

Example 2:

 
 
  1. Mysql_query ("set autocommit = 0"); // sets that mysql is not automatically submitted and must be submitted using the commit statement.
  2. $ SQL = "INSERT INTO 'user' ('id', 'username', 'sex') VALUES (NULL, 'test1', '0 ')";
  3. $ Sql2 = "insert into 'user' ('did', 'username', 'sex') VALUES (NULL, 'test1', '0 ')"; // I intentionally wrote an error.
  4. $ Res = mysql_query ($ SQL );
  5. $ Res1 = mysql_query ($ sql2 );
  6. If ($ res & $ res1 ){
  7. Mysql_query ("COMMIT ");
  8. Echo 'submission successful. ';
  9. } Else {
  10. Mysql_query ("ROLLBACK ");
  11. Echo 'data rollback. ';
  12. }
  13. Mysql_query ("END ");//TransactionsProcessingDo not forget mysql_query ("set autocommit = 1"); Submit automatically

Not SupportedTransactionsMyISAM engine database can use the table lock Method

// MyISAM & InnoDB support,
/*
Lock tables can LOCK the table used for the current thread. If the table is locked by other threads, blocking occurs until all the locks can be obtained.
Unlock tables can release any lock maintained by the current thread. When the thread publishes another lock tables, or when the connection to the server is closed, all TABLES locked by the current thread are implicitly unlocked.
*/

 
 
  1. Mysql_query ("lock tables 'user' WRITE"); // LOCK the 'USER' table
  2. $ SQL = "INSERT INTO 'user' ('id', 'username', 'sex') VALUES (NULL, 'test1', '0 ')";
  3. $ Res = mysql_query ($ SQL );
  4. If ($ res ){
  5. Echo 'submission successful .! ';
  6. } Else {
  7. Echo 'failed! ';
  8. }
  9. Mysql_query ("unlock tables"); // UNLOCK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.