为什么Hibernate会抛出org.hibernate.exception.LockAcquisitionException?

Abd*_*fid 11 java oracle spring hibernate transactions

我有这个方法:

mymethod(long id){  
    Person p = DAO.findPerson(id);

    Car car = new Car();
    car.setPerson(p);
    p.getCars().add(car);

    DAO.saveOrUpdate(car);
    DAO.saveOrUpdate(p);
    DAO.delete(p.getCars().get(0));//A person have many cars
}  
Run Code Online (Sandbox Code Playgroud)

映射:

Person.hbm.xml

<!-- one-to-many : [1,1]-> [0,n] -->
<set name="car" table="cars" lazy="true" inverse="true">
    <key column="id_doc" />
    <one-to-many class="Car" />
</set>

<many-to-one name="officialCar"
class="Car" 
column="officialcar_id" lazy="false"/>  
Run Code Online (Sandbox Code Playgroud)

Cars.hbm.xml

<many-to-one name="person" class="Person"
            column="id_person" not-null="true" lazy="false"/>   
Run Code Online (Sandbox Code Playgroud)

这种方法适用于单个线程,并且在多个线程上,给我一个错误:

02/08/2014 - 5:19:11 p.m. - [pool-1-thread-35] - WARN - org.hibernate.util.JDBCExceptionReporter - SQL Error: 60, SQLState: 61000 
02/08/2014 - 5:19:11 p.m. - [pool-1-thread-35] - ERROR - org.hibernate.util.JDBCExceptionReporter - ORA-00060: deadlock detection while waiting for a resource 
 
02/08/2014 - 5:19:11 p.m. - [pool-1-thread-35] - WARN - org.hibernate.util.JDBCExceptionReporter - SQL Error: 60, SQLState: 61000 
02/08/2014 - 5:19:11 p.m. - [pool-1-thread-35] - ERROR - org.hibernate.util.JDBCExceptionReporter - ORA-00060: deadlock detection while waiting for a resource 
 
02/08/2014 - 5:19:11 p.m. - [pool-1-thread-35] - ERROR - org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session 
org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update
Run Code Online (Sandbox Code Playgroud)

AOP交易:

<tx:advice id="txAdviceNomService" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
        <tx:method name="getAll*" read-only="true" propagation="SUPPORTS" />
        <tx:method name="find*" read-only="true" propagation="SUPPORTS" />
    </tx:attributes>
</tx:advice>
Run Code Online (Sandbox Code Playgroud)

注意:当我在更新后添加Thread.sleep(5000)时,它没问题.但这个解决方案并不干净.

Vla*_*cea 10

根据您的映射,操作顺序应如下所示:

Person p = DAO.findPerson(id);

Car car = new Car();
car.setPerson(p);

DAO.saveOrUpdate(car);

p.getCars().add(car);

Car firstCar = p.getCars().get(0);
firstCar.setPerson(null);
p.getCars().remove(firstCar);
if (p.officialCar.equals(firstCar)) {
   p.officialCar = null;
   p.officialCar.person = null;
}

DAO.delete(firstCar);
Run Code Online (Sandbox Code Playgroud)

更新删除装置获取独占锁,即使在READ_COMMITTED隔离级别.

如果另一个事务想要使用当前正在运行的事务更新同一行(已经锁定了该行),则不会出现死锁,而是锁定获取超时异常.

由于您遇到了死锁,这意味着您获取了多个表上的锁,并且锁定采集未正确排序.

因此,请确保服务层方法设置事务边界,而不是DAO方法.我看到你声明了getfind方法使用SUPPORTED,这意味着只有在当前启动时才会使用事务.我认为你也应该使用REQUIRED,但只需将它们标记为read-only = true.

因此,请确保事务方面在"mymethod"上应用事务边界而不在DAO上应用事务边界.


Abd*_*fid 3

我有汽车 -> (1 -n) 个地方。我在表位置(id_car)有一个外键。该外键没有索引。当我向该外键添加索引时,我的问题就解决了。

参考这个答案