0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FOR UPDATEでロックがかからない時

Last updated at Posted at 2018-11-10

FOR UPDATEでロックがかからない時。

一般的な方法

1.カウンタ取得(悲観的ロック)

SELECT count FROM counters FOR UPDATE;

2.インクリメント

count++

3.更新

UPDATE counters SET count = {#count};

通常は上記のような方法でロックをかけるが、上手くいかない場合。

######↓の方法であれば、接続毎に一意の識別子を生成・取得できる。

UPDATE counters SET count = LAST_INSERT_ID(count + 1);
SELECT LAST_INSERT_ID();

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?