在基本 MySQL 项目中使用哪种隔离级别?
嗯,我收到了一个作业[迷你项目],其中最重要的问题之一是数据库一致性。 该项目是一个 Web 应用程序,允许多个用户访问和使用它。我可以预期并发查询和更新请求到一小组表中,其中一些表将一个表连接到另一个表(使用外键)。
为了尽可能保持数据库的一致性,建议我们使用隔离级别。在阅读了一些关于它们的内容(可能还不够?)之后,我认为对我来说最有用的是 READ COMMITTED 和 SERIALIZABLE。
我可以将查询分为三种:
- 获取查询
- 更新查询
- Combo
对于第一种,我当然需要数据一致,我不想呈现脏数据,或未提交的数据等。因此,我想对这些查询使用 READ COMMITTED。 对于更新查询,我认为使用 SERIALIZABLE 将是最好的选择,但读了一点之后,我发现自己迷失了。 在组合中,我可能必须从数据库中读取数据,并决定是否需要/可以更新,这 2-3 个调用将在同一事务下进行。
想寻求一些建议,在每个查询选项中使用哪种隔离级别。我是否应该考虑每种类型的不同隔离级别?或者只坚持其中一个?
我正在使用 MySQL 5.1.53 以及 MySQL JDBC 3.1.14 驱动程序(要求...没有选择 JDBC 版本)
非常感谢您的见解!
编辑:
我决定使用REPEATABLE READ,这似乎是默认级别。 我不确定这是否是正确的方法,但我想可重复读取以及锁定共享模式和用于更新到查询应该可以正常工作...
你们觉得怎么样?
Well, I got an assignment [mini-project] in which one of the most important issues is the database consistency.
The project is a web application, which allows multiple users to access and work with it. I can expect concurrent querying and updating requests into a small set of tables, some of them connected one to the other (using FOREIGN KEYS).
In order to keep the database as consistent as possible, we were advised to use isolation levels. After reading a bit (maybe not enough?) about them, I figured the most useful ones for me are READ COMMITTED and SERIALIZABLE.
I can divide the queries into three kinds:
- Fetching query
- Updating query
- Combo
For the first one, I need the data to be consistent of course, I don't want to present dirty data, or uncommitted data, etc. Therefore, I thought to use READ COMMITTED for these queries.
For the updating query, I thought using SERIALIZABLE will be the best option, but after reading a bit, i found myself lost.
In the combo, I'll probably have to read from the DB, and decide whether I need/can update or not, these 2-3 calls will be under the same transaction.
Wanted to ask for some advice in which isolation level to use in each of these query options. Should I even consider different isolation levels for each type? or just stick to one?
I'm using MySQL 5.1.53, along with MySQL JDBC 3.1.14 driver (Requirements... Didn't choose the JDBC version)
Your insights are much appreciated!
Edit:
I've decided I'll be using REPEATABLE READ which seems like the default level.
I'm not sure if it's the right way to do, but I guess REPEATABLE READ along with LOCK IN SHARE MODE and FOR UPDATE to the queries should work fine...
What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议读已提交。一旦其他会话提交的数据被提交,就能够看到它们似乎很自然。
目前还不清楚为什么 MySQL 有一个默认的 REPEATABLE READ。
I would suggest READ COMMITTED. It seems natural to be able to see other sessions' committed data as soon as they're committed.
Its unclear why MySQL has a default of REPEATABLE READ.
我认为您过于担心隔离级别。
如果您有多个表需要更新,您需要这样做:
这是重要的事情,隔离级别只是肉汁。
默认的可重复读取级别就足够了。
请注意,
select ... for update
将锁定表,这可能会导致死锁,这比您可能尝试解决的问题更糟糕。仅当您要删除数据库中的行时才使用此选项。
老实说,我很少看到数据库中的行被删除,如果您只是进行更新,那么只需使用普通选择即可。
无论如何请参阅: http://dev.mysql.com/ doc/refman/5.0/en/innodb-transaction-model.html
I think you worry too much about the isolation level.
If you have multiple tables to update you need to do:
This is the important stuff, the isolation level is just gravy.
The default level of repeatable read will do just fine for you.
Note that
select ... for update
will lock the table, this can result in deadlocks, which is worse than the problem you may be trying to solve.Only use this if you are deleting rows in your DB.
To be honest I rarely see rows being deleted in a DB, if you are just doing updates, then just use normal selects.
Anyway see: http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html