MYSQL SELECT ... FOR UPDATE 不起作用?

发布于 2024-10-04 18:06:26 字数 523 浏览 2 评论 0原文

我遇到了 MySQL 的 SELECT .. FOR UPDATE 问题,这是我尝试运行的查询:

SQL = "SELECT * " +
      "FROM " + TableName + " " + 
      "WHERE out_status IN ("+outSStatus+") AND queued <= NOW() " +
      "ORDER BY out_status, id_queue ASC "+ limitSql+
      "FOR UPDATE";

此后,线程将执行 UPDATE 并将 out_status 更改为 99,然后它应该解锁该行。

我正在运行一个多线程java应用程序,因此有3个线程正在运行此SQL语句,但是当线程1运行此语句时,它不会锁定(隐藏)线程2和线程2的结果。 3. 因此线程2和线程2 3 得到相同的结果。

此外,每个线程都在其自己的 mysql 连接上。

有人可以帮我解决这个问题吗?或者也许有更好的解决方案?

非常感谢。

I am having issues with MySQL's SELECT .. FOR UPDATE, here is the query I am trying to run:

SQL = "SELECT * " +
      "FROM " + TableName + " " + 
      "WHERE out_status IN ("+outSStatus+") AND queued <= NOW() " +
      "ORDER BY out_status, id_queue ASC "+ limitSql+
      "FOR UPDATE";

After this, the thread will do an UPDATE and change the out_status to 99, which is then it should unlock the row.

I am running a multi-threaded java application, so 3 threads are running this SQL statement, but when thread 1 runs this, it doesn't lock (hide) its results from thread 2 & 3. Therefore threads 2 & 3 are getting the same results.

Also each thread is on its own mysql connection.

Can anybody please help me with this? OR perhaps have a better solution?

Much Appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必在意 2024-10-11 18:06:26

您可以使用事务

You can use transactions

绳情 2024-10-11 18:06:26

我有类似的问题。来自@Vikash 和@Adeel Ansari 的两个答案都是有效的建议。但是,我通过使用 InnoDB 引擎而不是 MyISAM 解决了这个问题。 MyISAM 只允许在表级别上锁定,而不允许在行级别上锁定。使用 InnoDB 时,您仍然需要事务。

I had a similar issue. Both answers, from @Vikash and @Adeel Ansari, are valid suggestions. However, I solved the issue by using InnoDB engine instead of MyISAM. MyISAM only allows locking on table level, not on row level. You still need transactions, when using InnoDB.

浮生面具三千个 2024-10-11 18:06:26

它可能正在工作,但你无法注意到。假设线程 1 执行了该语句,并自动提交了事务,因为自动提交已打开。当然,线程 2 也可以运行它。

尝试使用 Connection类的这个方法然后看结果。

It might be working, and you are unable to notice. Say thread 1 executed the statement, and committed the transaction automatically, because auto-commit was on. Then of course thread 2 would be able to run that too.

Try setting auto-commit off, using this method of Connection class and then see the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文