同时删除一组实体时的OptimisticLockException

时间:2014-10-24 15:11:32

标签: java hibernate google-app-engine concurrency ehcache

我正在尝试使用Hibernate创建一个简单的Servlet方法来删除一组对象。

List<MyEntity> entities = dao.getEntities(...); // just a query that returns a collection of managed entities

for (MyEntity entity : entities)
  dao.remove(entity); // em.remove(..) nothing more

一切都按预期工作但是...如果我使用jMeter并在几秒钟内查询我的精彩Servlet 200次,那么我可能会收到如下所示的异常。

我不乐意使用“从MyEntity中删除...”更新查询,因为我还使用了二级缓存。

例外:

[INFO] Caused by: javax.persistence.RollbackException: Error while committing the transaction
[INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
[INFO]  at xxx.xxx.xxx.TransactionTemplate.invokeWithinTransation(TransactionTemplate.java:23)
[INFO]  ... 42 more
[INFO] Caused by: javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
[INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1800)
[INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
[INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
[INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
[INFO]  ... 43 more
[INFO] Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
[INFO]  at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
[INFO]  at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
[INFO]  at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
[INFO]  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
[INFO]  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
[INFO]  at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
[INFO]  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
[INFO]  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
[INFO]  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
[INFO]  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
[INFO]  at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
[INFO]  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
[INFO]  at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
[INFO]  at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
[INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
[INFO]  ... 43 more

0 个答案:

没有答案