MYSQL - 锁定空行
如何锁定空行?
我想锁定一个空选择,例如:
会话 1
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users where username = 'nousername' for update;
Empty set (0.01 sec)
mysql>
会话 2
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users where username = 'nousername' for update;
Empty set (0.00 sec)
mysql>
我需要会话 2 等待会话 1 完成后再返回结果。
我怎样才能做到这一点?
谢谢?
How can i lock empty rows?
I want to lock an empty select for example:
Session 1
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users where username = 'nousername' for update;
Empty set (0.01 sec)
mysql>
Session 2
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users where username = 'nousername' for update;
Empty set (0.00 sec)
mysql>
I need Session 2 to wait until session 1 is completed before the result is returned.
How can i achieve that?
Thanks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有三个选项,每个选项都有优点和缺点,我更喜欢选择更新或锁定表:
1。 @Denis 提到的咨询锁
get_lock()
:缺点:
不会通过提交、回滚清除,
系统范围名称,因此您应该实现自己的唯一命名
优点:
不清除现有事务
不阻止其他查询
2.表锁
锁定表
优点:
确实会通过提交、回滚清除,
每个数据库锁定
缺点:
3。使用伪选择锁定表 - 我没有使用特定的选择,而是使用常规选择
从用户中选择 max(user_id) 进行更新
优点:
确实可以通过提交、回滚来清除,
每个数据库锁
不清除现有事务
缺点:
There you have three options, each having advantages and disadvantages, I prefer select for update or lock tables:
1. advisory lock
get_lock()
as mentioned by @Denis:cons:
doesn't get cleared away with commit, rollback,
system wide names, so you should implement your own unique naming
pros:
doesn't clear existing transactions
doesn't block other queries
2. table lock
lock tables
pros:
does get cleared away with commit, rollback,
per database locks
cons:
3. lock tables with pseudo select - Instead of having specific select, i use general select
select max(users_id) from users for update
pros:
does get cleared away with commit, rollback,
per database locks
doesn't clear existing transactions
cons:
使用咨询锁。
Use an advisory lock.