为什么以及何时应该在 Spring 事务隔离级别中使用 READ UNCOMMITTED 隔离级别

Why and when should I use READ UNCOMMITTED isolation level in Spring transaction isolation level

这可能是一个非常笼统的问题。但是,我仍然无法找到合适的 solution/answer 何时以及为什么我应该使用 READ UNCOMMITTED 隔离级别。大多数文章和 spring 文档都说,最有效的方法是使用 SERIALIZABLE。在那种情况下,为什么 spring 事务管理提出了 READ UNCOMMITTED 和 READ COMMITTED 隔离级别,如果它们效率不高的话。

我希望至少在这里我能得到答案。

提前致谢

首先我要说的是,我确实认同这样的信念,即很难遇到需要这种隔离级别的情况。 大多数时候,您希望从可重复读取或可序列化开始。


READ UNCOMMITTED 的一种可能(虽然疯狂)用途是用于 运行 不同数据库会话但都需要获取 "loose" 外键的前景索引的分布式系统。 假设您有两个服务,它们通过 REST 相互通信并通过相互协调来管理事务性:

  1. A插入一行并通过数据库生成的预期键调用B,然后等待B
  2. B 使用 A 提供的密钥插入另一个 table 以检索一些数据,然后提交自己的事务,最后 returns 成功供 A
  3. 使用
  4. A 提交自己的事务

此时,两个 table 中的行都已使用正确的键插入。然后将问题转移到步骤 (3) 中出现问题的极少数情况。

除其他事项外,我认为密钥不应由数据库生成...但是对于现有系统,您不一定有决定密钥生成位置的自由,也没有重新生成密钥的自由-即使你对实施的糟糕程度感到震惊,也要实施一些东西。


READ COMMITTED 的一种可能用途可能是一种软件,它向每个用户公开一个数据库会话,同时希望用户能够刷新内容并查看不同事务生成的新数据。一个很好的例子是数据库管理前端(Sql Server Management Studio、Toad、Squirrel 等等)。


下面的 link 更详细地解释了这些隔离级别在行业中的使用方式。此处也抄录一段,方便参考:

http://www.dbta.com/Columns/DBA-Corner/The-Danger-of-Dirty-Reads-98511.aspx

Programs that read database data can access numerous rows and are therefore susceptible to concurrency problems. To get around this issue, most major RDBMS products support read-through locks, also known as “dirty read” or “uncommitted read,” to help overcome concurrency problems. When using uncommitted reads (UR), an application program can access data that has been changed, but is not yet committed. Dirty read capability is commonly implemented using an isolation level, but the exact naming and implementation differs by DBMS vendor.

A program using dirty reads will read data without taking locks. This enables the application program to read data contained in the table as it is being manipulated. And it generally improves performance and availability of data because no locking mechanism is invoked during the process.

...

There are a few specific situations in which the dirty read capability may make sense. Consider the following cases:

  • Access is required to a reference, code, or look-up table that is basically static in nature. Due to the non-volatile nature of the data, a dirty read would be no different than a normal read the majority of the time. In those cases when the code data is being modified, any application reading the data would incur minimal, if any, problems.
  • Statistical processing must be performed on a large amount of data. For example, you may wish to determine the average age of female employees within a certain pay range. The impact of an uncommitted read on an average of multiple rows may be minimal because a single value changed may not greatly impact the result.
  • Dirty read can prove invaluable in a data warehousing environment. A data warehouse is used for online analytical processing and, other than periodic data propagation and/or replication, access is read-only. An uncommitted read is perfect in a read-only environment since it can cause little damage because the data is generally not changing.
  • In those rare cases when a table, or set of tables, is used by a single user only, UR can make a lot of sense. If only one individual can be modifying and accessing the data, locking is only adding overhead.
  • Finally, if the data being accessed is already inconsistent, little harm can be done using a dirty read to access the information.

The dirty read capability can provide relief to concurrency problems and deliver faster performance in very specific situations. Be certain to understand the implications of the UR isolation level and the “problems” it can cause before diving headlong into implementing it in your production applications.