Yii+mysql locking table method for preventing duplicate data in concurrent cases _php instance

Source: Internet
Author: User
Tags smarty template yii

This article illustrates the method of yii+mysql locking table to prevent duplication of data in case of concurrency. Share to everyone for your reference, specific as follows:

Lock Table Read lock

If a thread obtains a read lock on a table, the thread and all other threads can only read data from the table, and cannot do any write operations.

Lock tables User read;//read lock table
unlock tables;//unlock
lock tables User Read local;//Local read lock table, other thread inserts are not blocked, The update operation is blocked

Lock Table Write lock

If a thread gets a write lock on a table, only the thread that owns the lock can read and write the table from the table. The other threads are blocked.

Lock tables user write;//write lock table
unlock tables;//unlock

Examples of usages in Yii

/**
* Day single content status
/Public Function Getpointaready ($marke, $dayTime) {
  $model = Sysrun::model ()-> Findbyattributes (Array (' syr_marking ' => $marke, ' syr_daytime ' => $dayTime));
  if (empty ($model)) {
    //table write lock
    Yii::app ()->db->createcommand ()->settext ("Lock tables {Sys_run}}} WRITE ")->execute ();
    $model = new Sysrun ();
    $model->syr_marking = $marke;
    $model->syr_daytime = $dayTime;
    $model->syr_val = 0;
    $model->syr_subval = 0;
    $model->save ();
    Table Unlock
    Yii::app ()->db->createcommand ()->settext ("Unlock Tables")->execute ();
  }
  return $model;
}

For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to the YII framework and summary of common skills, the summary of PHP's excellent development framework, the Smarty Template Primer tutorial, and the summary of PHP Operations Office documentation (including Word,excel, ACCESS,PPT), "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the YII framework.

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.