MySQL table-Level Lock

Source: Internet
Author: User

When developing a project, you may encounter a problem: You must Randomly Read some records of a table and set update to unreadable, multiple people may randomly perform the same record at the same time and perform the update operation again, causing dirty Data Reading and repeated operations,

Therefore, consider locking the table. However, MyISAM is used, and transactions are not supported. Only table-level locks can be applied, and other users do not have the read permission. The following are two test files that run test in Firefox. PHP: Run test1.php in IE, and you will find that, within 10 seconds after Firefox runs, ie is in the waiting status, which is actually test. PHP locks the table, the webpage is not running, and the lock is not released. After 10 seconds, the lock is automatically released, and content is immediately displayed in IE, in this way, the records of the table are read and modified randomly, but the disadvantage is that other processes do not have the permission to read the table during the table lock, that is, when you operate a table record, the Administrator does not have the read permission. You still have to modify the table engine to InnoDB and use transactions to solve the problem.

 

Test1.php

 $ Db = new dB (); <br/> $ db-> locktable ("lock tables sbs_temp write "); <br/> $ SQL = "select * From sbs_temp limit 5"; <br/> $ rs = $ db-> getrs ($ SQL ); <br/> foreach ($ RS as $ row) {<br/> echo $ row ['query']. "<br>"; <br/>}</P> <p>

 

Test. php

 

$ Db = new dB (); <br/> $ db-> locktable ("lock tables sbs_temp write "); <br/> $ SQL = "select * From sbs_temp limit 5"; <br/> $ rs = $ db-> getrs ($ SQL ); <br/> foreach ($ RS as $ row) {<br/> echo $ row ['query']. "<br>"; <br/>}< br/> sleep (10); <br/>

Related Article

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.