mysql - 通过 MySQL InnoDB 行锁的应用程序互斥

标签 mysql architecture innodb mutex

我的应用程序由几个 Apache 服务器组成,这些服务器与一个通用的 MySQL 框通信。该应用程序的一部分允许用户在未来创建一小时的约会。我需要一种机制来防止不同的用户同时来自不同的 Apache 实例,预订相同的一小时预约时段。我已经看到在 Oracle 数据库上实现了类似的“系统间互斥”解决方案(基本上是“选择...更新”),但还没有处理对 MySQL 执行相同操作的细节。将不胜感激任何建议、代码或文档引用、最佳实践等。确实尝试谷歌搜索,但主要是关于 MySQL 内部互斥锁的讨论。

这些是我认为相关的我的 MySQL 设置(我的代码将有 try-catch 和所有并且在不解锁它锁定的内容的情况下永远不会放弃,但也必须考虑在这些情况下发生的情况):

mysql> show variables like 'innodb_rollback_on_timeout';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| innodb_rollback_on_timeout | OFF   | 
+----------------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 100   | 
+--------------------------+-------+
1 row in set (0.00 sec)

mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            1 | 
+--------------+
1 row in set (0.00 sec)

您可以推荐任何替代解决方案(MySQL 之外)?我也有一个正在运行的 memcached 实例,但经常被刷新(并且不确定我是否想要 memcachedb 等来实现容错)。

感谢您的帮助...

最佳答案

还可以使用 MySQL 和 MariaDB 的GET_LOCK(和RELEASE_LOCK)函数:

这些函数可用于实现问题中描述的行为。


获取锁 my_app_lock_1

SELECT GET_LOCK('my_app_lock_1', 1000); -- lock's name 'my_app_lock_1', timeout 1000 ms
+---------------------------------+
| GET_LOCK('my_app_lock_1', 1000) |
+---------------------------------+
|                               1 |
+---------------------------------+

释放锁:

DO RELEASE_LOCK('my_app_lock_1'); -- DO makes a result set ignored

请注意(引自 MariaDB 的文档):

  • >

    Names are locked on a server-wide basis. If a name has been locked by one client, GET_LOCK() blocks any request by another client for a lock with the same name. This allows clients that agree on a given lock name to use the name to perform cooperative advisory locking. But be aware that it also allows a client that is not among the set of cooperating clients to lock a name, either inadvertently or deliberately, and thus prevent any of the cooperating clients from locking that name. One way to reduce the likelihood of this is to use lock names that are database-specific or application-specific. For example, use lock names of the form db_name.str or app_name.str.

  • >

    Locks obtained with GET_LOCK() do not interact with transactions.

关于mysql - 通过 MySQL InnoDB 行锁的应用程序互斥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6132254/

相关文章:

php - 这是显示数据库表中数据的正确方法吗?

php - (Laravel) 错误号 : 150 "Foreign key constraint is incorrectly formed

mysql - 无法理解 ibd 文件加密如何帮助保护 MySql 中的数据

mysql - Mysql参数是否需要用括号括起来以防止sql注入(inject)?

mysql - 如何使用没有数据库名称的引擎对象查询数据库

REST,带有依赖于外部系统和 sql 的过滤器的分页

architecture - 去应用服务器?

database - 什么时候适合将 UUID 用于 Web 项目?

mysql - Mysql中的行级锁定

mysql - 在 Azure 数据库中为 MySQL 创建 MyISAM 表是否启用?