java 休眠异常 LockAcquisitionException:无法更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12807890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:18:29  来源:igfitidea点击:

hibernate exception LockAcquisitionException: could not update

javahibernatedeadlock

提问by John Powel

In my application I am using Java ,Hibernate(Annotations) and mySQL. It's working fine most of the time, but this error happens occasionally:

在我的应用程序中,我使用 Java、Hibernate(Annotations) 和 mySQL。大多数情况下它运行良好,但偶尔会发生此错误:

SEVERE: Could not synchronize database state with session
org.hibernate.exception.LockAcquisitionException: could not update: [myPack.Analysis$TestRun#5191]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:107)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2596)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2478)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2805)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:114)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at myPack.Analysis.fileHelper(Analysis.java:977)
    at myPack.Analysis.dirHelper(Analysis.java:702)
    at myPack.Analysis.dirHelper(Analysis.java:714)
    at myPack.Analysis.dirHelper(Analysis.java:714)
    at myPack.Analysis.dirHelper(Analysis.java:714)
    at myPack.Analysis.dirHelper(Analysis.java:714)
    at myPack.Analysis.dirHelper(Analysis.java:711)
    at myPack.Analysis.parseLog(Analysis.java:682)
    at myPack.Analysis.main(Analysis.java:614)

The most suspicious line in Analysis.javais:

中最可疑的一行Analysis.java是:

dbPerson = (Person) session.get(Person.class, dbPersonId, LockOptions.READ);

What I did is grab the row with the dbPersonIdand update/change the row contents as needed. For example:

我所做的是抓取行dbPersonId并根据需要更新/更改行内容。例如:

dbPerson.Name = "Hyman";
dbPerson.Age ++;
session.saveOrUpdate(dbPerson);

I am guessing something's wrong with the LockOptionsand I tried using different lockoptions such as NONE, UPGRADE... but still no luck.

我猜有什么问题,LockOptions我尝试使用不同的锁定选项,例如NONE, UPGRADE......但仍然没有运气。

The most frustrated thing is I can't even reproduce this error manually. It only happens once a while when Analysis.java are called multiple times.

最令人沮丧的是我什至无法手动重现此错误。它只会在多次调用 Analysis.java 时发生一次。

I know session is not thread-safe, but I am not sure whether this is related to that.

我知道 session 不是线程安全的,但我不确定这是否与此有关。

Any idea would be appreciated, thank you very much!

任何想法将不胜感激,非常感谢!

回答by John Powel

Since it is using a READlock option, I catch the exception ,let the thread sleep for a while and then try to acquire the lock again, which solves the problem:

由于它使用了READ锁选项,我捕获了异常,让线程休眠一段时间,然后再次尝试获取锁,从而解决了问题:

try {
  dbPerson = (Person) session.get(Person.class, dbPersonId, LockOptions.READ);
} catch (RuntimeException e) {
  if (e instanceof LockAcquisitionException) {
    try {
      Thread.sleep(15000);
      dbPerson = (Person) session.get(Person.class, dbPersonId, LockOptions.READ);                      
     } else {
      throw e;
     }