简体   繁体   English

在Web服务器级别锁定MySQL表

[英]Locking MySQL table at web server level

Suppose I have a web app (using Django on the server side) and I would like one of the users (admin-like user "A" ) to be able to load a certain view (webpage) where she can modify the database tables which will prevent other users to accessing the database (and views associated with modifying the database) until the user A is finished (presses certain button on the page). 假设我有一个Web应用程序(在服务器端使用Django),我希望其中一个用户(类似管理员的用户“A” )能够加载某个视图(网页),在那里她可以修改数据库表。将阻止其他用户访问数据库(以及与修改数据库相关的视图),直到用户A完成(按下页面上的某个按钮)。 I am using MySQL and also SQLite (for development). 我正在使用MySQL和SQLite(用于开发)。 Here are my questions. 这是我的问题。

  1. MySQL mechanism of locking tables works on the MySQL server level and not on the web server level. 锁定表的MySQL机制在MySQL服务器级别上工作,而不在Web服务器级别上工作。 Since the application server (Django) side has only one MySQL user associated with the MySQL server (ie all the app users will have the same user on the MySQL side), that mechanism is irrelevant here. 由于应用程序服务器(Django)端只有一个与MySQL服务器关联的MySQL用户(即所有应用程序用户在MySQL端都有相同的用户),因此这种机制无关紧要。 Am I correct? 我对么?

  2. If 1. above is correct, what would be the right way to create such a lock? 如果1.以上是正确的,那么创建这样一个锁的正确方法是什么? Well, one could create a separate table "lock_table" which would just have a single Boolean entry and every view would poll it and if it is set - redirect to "db temporarily locked" view. 好吧,可以创建一个单独的表“lock_table”,它只有一个布尔条目,每个视图都会轮询它,如果设置了 - 重定向到“db temporary locked”视图。 However, I am not sure it is a good way. 但是,我不确定这是一个好方法。 Please, advise on how to implement it if you know. 如果您知道,请告知如何实施它。

Thanks. 谢谢。

  1. You're correct. 你说的没错。 You cannot set a system on the MySQL level for locking tables as every database query will be made by the same MySQL user. 您无法在MySQL级别设置系统来锁定表,因为每个数据库查询都将由同一个MySQL用户进行。

  2. The right way would be to set a lock in cache using memcached or another cache system, and then checking if such a lock existed in a decorator on those views. 正确的方法是使用memcached或其他缓存系统在缓存中设置锁定,然后检查这些视图中的装饰器中是否存在此类锁定。

Choice number two would be a good bet. 第二选择将是一个不错的选择。 Another option would be to have a "virtual second database" defined in your django databases and use this database for executing the lock query. 另一个选择是在django数据库中定义“虚拟第二个数据库”,并使用该数据库执行锁定查询。 See here for details. 详情请见此处

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

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