[英]Do I need a transaction when updating a row with a specific condition
I am working on a small project and I want to flag some items in a table as "claimed".我正在做一个小项目,我想将表格中的某些项目标记为“已声明”。
So most items would have a claim_id = NULL, when I want to claim an item I will call this command to update the next available item:所以大多数物品都会有一个claim_id = NULL,当我想认领一个物品时,我会调用这个命令来更新下一个可用的物品:
update ci_items
set claim_id = ?
where claim_id IS NULL
order by id
LIMIT 1;
I think this should always either update the next available item, or nothing if all items have been claimed.我认为这应该总是更新下一个可用的项目,或者如果所有项目都已被认领,则什么都不更新。 In the context of a web application where 2 db connections could run this at the same time, is there any risk that an item gets claimed twice at the same time, one claim_id overriding the previous one?
在 web 应用程序的上下文中,2 个 db 连接可以同时运行,是否有任何风险同时声明一个项目两次,一个 claim_id 覆盖前一个?
I am thinking I could use a transaction but the table structure is currently MyISAM and I don't think that supports transactions我想我可以使用事务,但表结构目前是 MyISAM,我认为不支持事务
What you have show us works well.您向我们展示的内容效果很好。 Individual SQL update queries have an implicit transaction (also known as "autocommit") for their duration.
单个 SQL 更新查询在其持续时间内具有隐式事务(也称为“自动提交”)。
Go for it. Go 为它。
And don't stop asking yourself this kind of Atomicity, Consistency, Isolation, Durability ( ACID ) question as you design and build your application.在设计和构建应用程序时,不要停止问自己这种原子性、一致性、隔离性、持久性 ( ACID ) 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.