两个线程更新同一个对象时可能出现hibernate异常? [英] Possible hibernate exceptions when two threads update the same Object?

查看:182
本文介绍了两个线程更新同一个对象时可能出现hibernate异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有两个线程更新同一个对象时,有人能帮助我解决可能的休眠异常吗?
$ b

例如:名称为a,年龄为30且地址为test的员工
thread1尝试将a更新为b,并且thread2尝试将a更新为c



在此先感谢,
Kathir

解决方案

感谢您的回答,下面是观察和分析后的评论。


  1. 我们也可以使用查询中的where子句进行条件更新并使用executeUpdate()方法。例如:Hibernate-Query-executeUpdate()方法更新并返回更新的实体数目。所以如果executeUpdate()返回零,这意味着行已经被另一个线程更新了。 (无例外)


  2. 使用@Version。 (OptimisticLockException)


  3. 使用行级DB锁定。 (数据库例外)


  4. 使用同步。 (Java同步异常)



Could someone help me with the possible hibernate exceptions when two threads update the same Object?

ex: employee with name "a", age "30" and address "test" thread1 tries to update "a" to "b" and thread2 tries to update "a" to "c"

Thanks in advance, Kathir

解决方案

Thanks for the answers and below are the comments after observation and analysis

  1. We can also do a conditional update with where clause in the query and use executeUpdate() method. Ex: The Hibernate - Query - executeUpdate() method updates and return the number of entities updated. So if the executeUpdate() returns "zero" it means the row has been already updated by another thread. (No Exception)

  2. Using @Version. (OptimisticLockException)

  3. Using Row Level DB lock. (DB Exception)

  4. Using Synchronization. (Java Synchronization Exception)

这篇关于两个线程更新同一个对象时可能出现hibernate异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆