i have a persistent hibernate managed @entity Obj in Hibernate, which has fields id, fieldA, and fieldB, among others.

我在hibernate中有一个持久的hibernate托管@entity Obj,它有字段id、fieldA和fieldB等。

In some class i have two @Transactional methods, updateA() and updateB().
updateA() obtains an Obj by its id, does some processing, and updates its fieldA.
updateB does same for fieldB.

在某些类中,我有两个@Transactional方法:updateA()和updateB()。updateA()通过其id获取Obj,进行一些处理,并更新其fieldA。updateB对fieldB也这样做。

I also have two clients that continuously make requests. One client always makes requests that call updateA(), and the other client always calls updateB().
They both pass the same id of an existing Obj.

我也有两个客户不断地提出请求。一个客户机总是发出调用updateA()的请求,而另一个客户机总是调用updateB()。它们都传递现有Obj的相同id。

Is there a way to avoid the HibernateOptimisticLockingFailureException that keeps occurring, or to handle it once successfully, given the fact that each method updates different fields? Some kind of a merge?

有什么方法可以避免经常发生的HibernateOptimisticLockingFailureException,或者在每个方法更新不同字段的情况下成功地处理它吗?某种合并?

Just to complete the picture, the thread that calls updateA() in fact calls other transactional methods similar to updateA(), but none updates fieldB.

为了完成这幅图,调用updateA()的线程实际上调用与updateA()类似的其他事务方法,但没有一个线程更新fieldB。

At first I just tried to catch the exception and re-try the operation a second time. But sometimes it fails the second time too... This does not seem like a good solution.

起初,我只是试图捕获异常,然后再次尝试操作。但有时第二次也失败了……这似乎不是一个好的解决方案。

2 个解决方案

#1


1

If you can safely do it, the easiest way is to exclude fieldB from optimistic locking:

如果您可以安全地进行此操作,最简单的方法是将fieldB排除在乐观锁定之外:

import org.hibernate.annotations.OptimisticLock;

/* ... */

@OptimisticLock(excluded=true)
private B fieldB;

更多相关文章

  1. Java Quartz的使用方法,实现程序计时
  2. 将行计数器方法与字数统计方法相结合
  3. java基础:集合框架之Map(共性方法)
  4. JAVA中的反射只获取属性的get方法
  5. 再论javaIO之拷贝MP3(read方法返回int的原因)
  6. java基础-Arrays类常用方法介绍
  7. eclipseIDE for Java EE 下 离线 安装CDT的方法
  8. 在servlet中的init方法得到了对数据库操作的值,怎么传给前端
  9. 【JAVA】用java编写程序时总提示缺少方法主体或声明抽象的原因

随机推荐

  1. Android studio 下载地址
  2. Android Calander Event
  3. android的布局水平与垂直显示
  4. Android Choreographer 初始化
  5. android:password is deprecated: Use in
  6. 处理启动页白屏问题
  7. Android自带图标库
  8. Android 基本UI控件
  9. sadsad
  10. listview的特殊属性