简体   繁体   English

如何在MySQL InnoDB表中的行上放置长期锁定?

[英]How to put a long-term lock on a row in a MySQL InnoDB Table?

I've searched this for a while and couldn't find much. 我已经搜索了一段时间,但找不到很多。

Is it possible to lock individual rows manually on an InnoDB table in MySQL? 是否可以在MySQL中的InnoDB表上手动锁定各个行?

Basically we have a table where some records are processed and should no longer be edited. 基本上,我们有一个表,其中处理了一些记录,不应再对其进行编辑。 They need to be kept in the state they're currently in, but still be readily accessible in terms of reading the data. 它们需要保持当前状态,但就读取数据而言仍很容易访问。

We already have a "locked" column which is 0 or 1, but somehow , it's being edited when it shouldn't be. 我们已经有一个“锁定”列,该列为0或1,但不知何故 ,当它不应该被编辑时。 Rather than relying on the code to behave nicely, we were wanting to put a hard lock on it at a MySQL level, so these can't be changed, even if our PHP code tries to somewhere. 我们不是要依靠代码来表现良好,而是要在MySQL级别对其进行硬锁定,因此即使我们的PHP代码尝试到某个地方, 也不能更改这些代码。

Is this possible? 这可能吗?

No. 没有。

To do this correctly, use application partitioning such that your application stores active data in one table and historical data in another, and use permissions to specify that your application can read one table and read/write on another. 若要正确执行此操作,请使用应用程序分区,以便您的应用程序将活动数据存储在一个表中,并将历史数据存储在另一个表中,并使用权限指定您的应用程序可以读取一个表并可以在另一个表上进行读/写。

Alternatively, add an access layer so that you read through views and write through stored procedures, and have the SP control whether a row can be written based on the field value. 或者,添加访问层,以便您可以浏览视图并写入存储过程,并让SP控制是否可以基于字段值写入行。

Either way you can't do this with MySQL natively without writing some code somewhere 无论哪种方式,如果没有在某处编写一些代码,就无法使用MySQL本机执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM