如果应用程序 srv 隔离级别设置为 READ COMMITTED,会发生 OptimisticLockException 吗?

发布于 2024-09-26 12:09:02 字数 487 浏览 10 评论 0原文

我正在使用带有“OpenJPA 1.2.3-SNAPSHOT”的 Websphere 应用程序服务器 7.0.0.0.9。 我已设置 jdbc 数据源 webSphereDefaultIsolationLevel=2 的属性(READ COMMITTED)。 我有这个问题,因为我的理解是,如果存在多个线程提交同一行的竞争,就会发生 OptimasticLockException。 但我认为如果隔离级别应用程序服务器设置为 READ COMMITTED,这种情况永远不会发生。

这是我遇到的异常..

<openjpa-1.2.3-SNAPSHOT-r422266:907835 fatal store error> org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock violation was detected when flushing object instance 

I am using Websphere application server 7.0.0.0.9 with ;OpenJPA 1.2.3-SNAPSHOT'.
I have Set property of jdbc data source webSphereDefaultIsolationLevel=2 (READ COMMITTED).
I have this question because My understanding is the OptimasticLockException occurs if there is race to commit the same row by multiple thread.
But I think this situation should never occur if isolation level app server is set to READ COMMITTED.

This is exception I am getting..

<openjpa-1.2.3-SNAPSHOT-r422266:907835 fatal store error> org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock violation was detected when flushing object instance 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-10-03 12:09:02

我有这个问题,因为我的理解是,如果存在多个线程提交同一行的竞争,就会发生OptimisticLockException

是的,如果实体的 Version 属性在提交时比读取时更高(即已被另一个事务修改),则会引发 OptimisticLockException。下图说明了这一点(借自 JPA 2.0 并发和锁定):

alt text

但我认为如果隔离级别应用服务器设置为 READ COMMITTED,这种情况永远不会发生。

为什么?使用 READ COMMITTED 隔离级别时,不可重复读取可能会发生,但是:

  1. 这不会影响内存数据的表示(上例中的e1
  2. 大多数时候,您不会重新读取数据
  • 即使您这样做(并执行不可重复的读取),实体仍然可能会被修改在提交更改之前由另一个线程执行。

总而言之,READ COMMITTED 不会阻止 OptimisticLockException

I have this question because my understanding is the OptimisticLockException occurs if there is race to commit the same row by multiple thread.

Yes, an OptimisticLockException will be thrown if the Version attribute of an entity is higher (i.e. has been modified by another transaction) at commit time than when it was read. This is illustrated by the figure below (borrowed from JPA 2.0 Concurrency and locking):

alt text

But I think this situation should never occur if isolation level app server is set to READ COMMITTED.

Why? When using a READ COMMITTED isolation level, non-repeatable reads may occur but:

  1. This won't affect in memory representation of data (e1 in the above example)
  2. Most of time, you don't re-read data
  • And even if you do (and perform a non-repeatable read), an entity might still get modified by another thread before you commit changes.

To sum up, READ COMMITTED won't prevent OptimisticLockException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文