Data optimistic lock in PHP concurrency process

Source: Internet
Author: User

Causes of concurrency: There is a call Zhang San Expert is very popular, assuming Zhang San in this Wednesday 9 o'clock in the morning to 10 points between the consultation schedule, the system backstage will cost Wednesday 9 o'clock to 10 points of 10 expert number, waiting for the patient to set the number, but Zhang San too hot, the result has 10,000 patients to set the ticket

Hypothetical database design

UID (Zhang San) time (duration) number (Shift No.)

When a request comes over, Zhang San doctor's shift number will be reduced by one, here the process assumes that there are 10,000 requests to request Zhang San doctor's shift number, no request will open up a process, there will be 10,000 processes at the same time for the Zhang San doctor's shift number, due to the operating system CPU is constantly switching, waiting, wake up, .... (Specifically, you can learn about multithreaded programming). This can cause multiple process security issues.

This is the time to add an optimistic lock to solve the problem, add Verison (version number) in the database table (Accord)

ID UID Time Number (roster) version (version number) status (status)

1 Tri 9:00-9:10 z-0001 001 0

2 Tri 9:10-9:20 z-001 002 0

......

PHP Code:

<?php

$doctor = $_get[' doctor_id '];//accept the doctor's UID

$number = $_get[' number '];//

mysql_query ("Begin");//start MySQL transaction

$number =5;//Define the number of queries to avoid a dead loop

while (True && + + $i)

{

if ($i < $num)

{


try{

$sql = "Select Version,id,number from Accord where uid=". $doctor. "and status=0 limit 1";

Suppose $database_obj->query ($sql); Execute SQL statements directly

This time we got Zhang San's shift number, assuming it's got the ID 1 number.

$data = $database _obj->query ($sql);

Coding .....

When your business logic is done to update the status of the ID 1 shift number

$sql = "Update accord set Status=1 where version=". $data [' Version ']. "and id=". $data [' id '];

$data = $database _object->query ($sql);

if (false== $data)

{

throw new Exception ();//Throws an exception

}

If successful, submit

mysql_query (' commit ');

Key: This time due to the concurrent data requests, the CPU does not stop switching, the process in the execution of the operation of the operating system to the other process, the current process is in the waiting state, the ID of a schedule state is modified to occupy, SQL statement execution failed

}catch (Exception $e)

{

mysql_query (' rollback ');

}

mysql_query (' End ');

}

Break

?>

The above is the principle of optimistic locking, but according to their own business needs can make the following changes, individuals think optimistic lock is not the best way to deal with concurrency data. Here's how the queue is presented




This article is from the "10920471" blog, please be sure to keep this source http://10930471.blog.51cto.com/10920471/1885900

Optimistic data locking during PHP concurrency

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.