如何锁定 MySQL 中的一行?
我正在开发一款 CRM 桌面应用程序,该应用程序一次将由多个代理使用,并且所有代理都将浏览相同的客户列表。我在这里需要做的是避免代理之间的冲突,因此一旦代理从列表中选择了客户,其他人就不应该再看到该行,或者换句话说,他们不应该能够选择该客户排直到第一个代理完成!想到的最简单的方法可能听起来很愚蠢,那就是添加两个字段 LOCK(BIT)、LOCK_EXPIRY(DATETIME) 并对其进行管理。我不知道,但我认为应该有另一种方法来锁定特定会话的行。我在 Google 上搜索,发现了两种 InnoDB 锁定方法,但我不确定这些方法在这种情况下是否可以帮助我。
I'm working on a CRM desktop application which is going to be used by more than one agent at a time, and all agents will go through the same list of customers. What I need to do here is to avoid conflicts between agents, so once an agent selects a customer from the list, others shouldn't be able to see that row anymore, or in another words they shouldn't be able to select that customer row until the first agent is done ! The simplest way that comes in mind wich may sound stupid is to add two fields LOCK(BIT), LOCK_EXPIRY(DATETIME) and manage that. I don't know But I think there should be another way to lock a row for particular session. I searched on Google and I found two InnoDB locking methods but I'm not sure if those can help me here in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以在 MySQL 中使用 GET_LOCK() 函数:
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
I think you can use GET_LOCK() function with MySQL:
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
我建议您添加您描述的两个字段,除了将 LOCK(BIT) 替换为 LOCKED_BY(AGENT_ID) 之外。否则,如果锁定客户列表的代理刷新他/她的页面,则锁定的行可能会消失,直到锁定过期。
I suggest you add the two fields you described, except replace LOCK(BIT) with LOCKED_BY(AGENT_ID). Otherwise if the agent that has locked the list of customers refreshes his/her page, the locked rows may disappear until the lock expires.