收到两个线程,两个线程都在操作同一条数据,a线程来了后先去查询数据的时候b线程也在查询数据,两个线程都是操作的同一条数据
a线程查完后就把数据删除了,b线程再去删除时没有就会抛出
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; statement executed: delete from 表名 where id=?; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; statement executed: delete from 表名 where id=?
我这里的原因是,同时收到两个事件,两个事件都开启了异步处理,但是两个事件都是调用了同一个方法,在方法上加了@Transactional(propagation = Propagation.REQUIRES_NEW)
Propagation.REQUIRES_NEW
创建一个新的事务,如果当前存在事务,则把当前事务挂起。也就是说不管外部方法是否开启事务,Propagation.REQUIRES_NEW修饰的内部方法会新开启自己的事务,且开启的事务相互独立,互不干扰。
解决方法:
去掉异步,和Propagation.REQUIRES_NEW 解决问题