lock(true)->save($data);"."/> lock(true)->save($data);".">

Home>Article>PHP Framework> How to lock the table in thinkphp

How to lock the table in thinkphp

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼 Original
2019-08-22 11:33:27 4649browse

How to lock the table in thinkphp

Recently, I need to write a registration system that does not have a large amount of concurrency, but it is still possible for multiple people to sign up at the same time. Because registration involves order, if rows or tables are not locked, the following situation will occur:

Example:

Only user A has signed up in the current system, and the order is 1. Record is (A, 1);

At a certain time t, user B, user C, and user D registered at the same time. The backend received the registration requests of B, C, and D almost at the same time, so it queried the registration form and found that, Currently there is only (A, 1), so insert (B, 2), (C, 2), (D, 2) into the registration form to get the result:

(A, 1)

(B, 2)

(C, 2)

(D, 2).

The first thing I thought of was: I don’t record the order, I record the insertion timestamp. In this case, it is

(A, ta)

(B, tb)

(C, tc)

(D, td).

It is almost impossible for ta, tb, tc, and td to be equal.

Related recommendations: "ThinkPHP Tutorial"

However, there are three problems that cannot be solved by doing this:

1. The timestamps may be equal. , the probability is very low;

2. It is not intuitive and needs to be sorted by timestamp;

3. Use a limit on the number of people when registering, such as limiting registration to 40 people, and immediately feedback whether the current registration is registered. superior.

The current solution is to solve it through database locking.

I checked a lot of information online and found that ThinkPHP can add pessimistic locking and optimistic locking. The target system has a small amount of access, so just use pessimistic locking.

MyISAM can only lock tables, while InnoDB can lock rows. Just lock the table on the target system.

The lock table scheme given in the official document is:

$User->lock(true)->save($data);// 使用悲观锁功能

However, the target system needs to perform a series of operations, so the lock table code used is:

M()->query("lock tables yourtable write"); // TODO // your code M()->query("unlock tables");

The actual effect is running The effect is not bad [the real date has been filtered out]:

??-??-?? 10:00:00 1 ??-??-?? 10:00:00 2 ??-??-?? 10:00:00 3 ??-??-?? 10:00:00 4 ??-??-?? 10:00:01 5 ??-??-?? 10:00:01 6 ??-??-?? 10:00:01 7 ??-??-?? 10:00:01 8 ??-??-?? 10:00:02 9 ??-??-?? 10:00:02 10 ??-??-?? 10:00:02 11 ??-??-?? 10:00:02 12 ??-??-?? 10:00:02 13 ??-??-?? 10:00:02 14 ??-??-?? 10:00:03 15 ??-??-?? 10:00:03 16 ??-??-?? 10:00:03 17 ??-??-?? 10:00:05 18 ??-??-?? 10:00:06 19 ??-??-?? 10:00:07 20 ??-??-?? 10:00:08 21 ??-??-?? 10:00:10 22 ??-??-?? 10:00:15 23 ??-??-?? 10:00:17 24 ??-??-?? 10:00:19 25 ??-??-?? 10:00:19 26 ??-??-?? 10:00:24 27 ??-??-?? 10:00:25 28 ??-??-?? 10:00:34 29 ??-??-?? 10:00:35 30 ??-??-?? 10:00:38 31 ??-??-?? 10:01:06 32 ??-??-?? 10:01:11 33 ??-??-?? 10:01:11 34 ??-??-?? 10:01:17 35 ??-??-?? 10:01:18 36 ??-??-?? 10:02:27 37 ??-??-?? 10:02:38 38 ??-??-?? 10:02:39 39 ??-??-?? 10:02:57 40

The above is the detailed content of How to lock the table in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn