两个线程更新同一个对象时可能出现hibernate异常? [英] Possible hibernate exceptions when two threads update the same Object?
问题描述
$ b
例如:名称为a,年龄为30且地址为test的员工
thread1尝试将a更新为b,并且thread2尝试将a更新为c
在此先感谢,
Kathir
感谢您的回答,下面是观察和分析后的评论。
-
我们也可以使用查询中的where子句进行条件更新并使用executeUpdate()方法。例如:Hibernate-Query-executeUpdate()方法更新并返回更新的实体数目。所以如果executeUpdate()返回零,这意味着行已经被另一个线程更新了。 (无例外)
-
使用@Version。 (OptimisticLockException)
-
使用行级DB锁定。 (数据库例外)
-
使用同步。 (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
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)
Using @Version. (OptimisticLockException)
Using Row Level DB lock. (DB Exception)
Using Synchronization. (Java Synchronization Exception)
这篇关于两个线程更新同一个对象时可能出现hibernate异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!