【问题标题】:Mysql InnoDB locking for atomic readMysql InnoDB 锁定原子读取
【发布时间】:2013-10-07 11:20:59
【问题描述】:

我有一个网站,它有一个方法(在支持 bean 中),它执行几个 READ 请求(例如检查用户的权限),然后插入一些数据。

程序如下所示:

1.) SELECT * FROM userLocks (table which logs the users which are exclusively working on a topic, to avoid redundancy). it checks if there is currently no lock (user working) for the topic.
2.) SELECT * FROM ... (some other selects for further checking)
3.) INSERT INTO userLocks (curUser, timeout) values (...) (if everything was ok, create a new lock for the current user for the topic)

我现在面临的问题是:在第一次选择和插入之间,许多其他用户可以请求操纵表 userLocks 数据的同一网站。它似乎不是线程安全的。

这个问题有解决办法吗?我只需要在此过程中为其他用户锁定所有使用过的表。 (另一个请求应该等到锁被释放,这不会超过半秒......)

(我使用 InnoDB)

【问题讨论】:

  • 你关闭了自动提交吗?

标签: mysql jdbc locking innodb


【解决方案1】:

我建议您需要稍微研究一下 SQL 事务以及 InnoDB 的行和表锁。这是一篇帮助我的文章https://blogs.oracle.com/mysqlinnodb/entry/introduction_to_transaction_locks_in

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2011-10-03
    相关资源
    最近更新 更多