接続のためにMysqlとofc hibernateでHazelcastを使用しています。データを処理し、hazelcast と db の両方から削除しようとするマルチスレッド アプリケーションがあります。
しかし、しばらくすると、私は得ています
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
これは、アプリケーションが処理後にデータベースからデータを削除しようとしているときに発生します。この例外の詳細なエラー ログは次のとおりです。
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) [hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) [hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189) [hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate3.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) [hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795) [hibernate3.jar:3.6.0.Final]
at com.mypackage.myDao.removeFinishedJob(myDao.java:113) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:179) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:29) [bundlefile:na]
at com.hazelcast.impl.concurrentmap.MapStoreWrapper.delete(MapStoreWrapper.java:107) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$RemoveOperationHandler$RemoveStorer.doMapStoreOperation(ConcurrentMapManager.java:2616) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$AbstractMapStoreOperation.run(ConcurrentMapManager.java:3747) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.executor.ParallelExecutorService$ParallelExecutorImpl$ExecutionSegment.run(ParallelExecutorService.java:212) [hazelcast-all-2.3.1.jar:2.3.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
at com.hazelcast.impl.ExecutorThreadFactory$1.run(ExecutorThreadFactory.java:38) [hazelcast-all-2.3.1.jar:2.3.1]
2013-06-02 00:28:29.831 [hz.MyProject.cached.thread-5] ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate3.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) [hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795) [hibernate3.jar:3.6.0.Final]
at com.mypackage.myDao.removeFinishedJob(myDao.java:113) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:179) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:29) [bundlefile:na]
at com.hazelcast.impl.concurrentmap.MapStoreWrapper.delete(MapStoreWrapper.java:107) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$RemoveOperationHandler$RemoveStorer.doMapStoreOperation(ConcurrentMapManager.java:2616) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$AbstractMapStoreOperation.run(ConcurrentMapManager.java:3747) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.executor.ParallelExecutorService$ParallelExecutorImpl$ExecutionSegment.run(ParallelExecutorService.java:212) [hazelcast-all-2.3.1.jar:2.3.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
at com.hazelcast.impl.ExecutorThreadFactory$1.run(ExecutorThreadFactory.java:38) [hazelcast-all-2.3.1.jar:2.3.1]
2013-06-02 00:28:29.834 [hz.MyProject.cached.thread-5] ERROR c.t.s.m.s.i.myDao - fail to delete quest: 137010414975000410
javax.persistence.OptimisticLockException: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.ejb.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1255) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1166) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:798) ~[hibernate3.jar:3.6.0.Final]
at com.mypackage.myDao.removeFinishedJob(myDao.java:113) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:179) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:29) [bundlefile:na]
at com.hazelcast.impl.concurrentmap.MapStoreWrapper.delete(MapStoreWrapper.java:107) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$RemoveOperationHandler$RemoveStorer.doMapStoreOperation(ConcurrentMapManager.java:2616) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$AbstractMapStoreOperation.run(ConcurrentMapManager.java:3747) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.executor.ParallelExecutorService$ParallelExecutorImpl$ExecutionSegment.run(ParallelExecutorService.java:212) [hazelcast-all-2.3.1.jar:2.3.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
at com.hazelcast.impl.ExecutorThreadFactory$1.run(ExecutorThreadFactory.java:38) [hazelcast-all-2.3.1.jar:2.3.1]
2013-06-02 00:28:29.834 [hz.MyProject.cached.thread-5] ERROR c.t.s.m.s.i.myDao - fail to delete quest: 137010414975000410
javax.persistence.OptimisticLockException: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.ejb.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1255) ~[hibernate3.jar:3.6.0.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1166) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:798) ~[hibernate3.jar:3.6.0.Final]
at com.mypackage.myDao.removeFinishedJob(myDao.java:113) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:179) [bundlefile:na]
at com.mypackage.myDao.delete(myDao.java:29) [bundlefile:na]
at com.hazelcast.impl.concurrentmap.MapStoreWrapper.delete(MapStoreWrapper.java:107) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$RemoveOperationHandler$RemoveStorer.doMapStoreOperation(ConcurrentMapManager.java:2616) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.ConcurrentMapManager$AbstractMapStoreOperation.run(ConcurrentMapManager.java:3747) [hazelcast-all-2.3.1.jar:2.3.1]
at com.hazelcast.impl.executor.ParallelExecutorService$ParallelExecutorImpl$ExecutionSegment.run(ParallelExecutorService.java:212) [hazelcast-all-2.3.1.jar:2.3.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
at com.hazelcast.impl.ExecutorThreadFactory$1.run(ExecutorThreadFactory.java:38) [hazelcast-all-2.3.1.jar:2.3.1]Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) ~[hibernate3.jar:3.6.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795) ~[hibernate3.jar:3.6.0.Final]
... 11 common frames omitted
そして、これはdaoです:
public class MyDao implements MapStore, MapLoaderLifecycleSupport {
private EntityManager em;
private static EntityManagerFactory emf;
private static final Logger LOG = LoggerFactory.getLogger(MyDao.class);
private static final ThreadLocal ems = new ThreadLocal();
public static void setEmf(EntityManagerFactory emf) {
MyDao.emf = emf;
}
public static MyDao getInstance() {
EntityManager em = getEntityManager();
return new MyDao(em);
}
private static EntityManager getEntityManager() {
EntityManager em = ems.get();
if (em == null || !em.isOpen()) {
em = emf.createEntityManager();
ems.set(em);
}
return em;
}
@Inject
public MyDao(EntityManager em) {
super();
this.em = em;
}
public MyDao() {
this(getEntityManager());
LOG.info("Hazelcast MapStorer init");
}
@SuppressWarnings("unchecked")
public List getAllUnfinishedJobs() {
em = getEntityManager();
return em.createQuery("from MyEntity req where req.status=?1").setParameter(1, MyEntity.Status.UNFINISHED).getResultList();
}
public void updateFinishedJob(long reqEntityId) {
if (reqEntityId loadAll(Collection paramCollection) {
Map tmpMap = new HashMap();
for (Long key : paramCollection) {
tmpMap.put(key, load(key));
}
return tmpMap;
}
@Override
public Set loadAllKeys() {
em = getEntityManager();
List ids = em.createQuery("select req.id from MyEntity req").getResultList();
Set keys = new HashSet(ids);
return keys;
}
@Override
public void store(Long paramK, MyEntity paramV) {
paramV.setId(paramK);
saveUnfinishedJob(paramV);
}
@Override
public void storeAll(Map paramMap) {
for (Map.Entry en : paramMap.entrySet()) {
store(en.getKey(), en.getValue());
}
}
@Override
public void delete(Long paramK) {
removeFinishedJob(paramK);
}
@Override
public void deleteAll(Collection paramCollection) {
for (Long key : paramCollection) {
delete(key);
}
}
@Override
public void init(HazelcastInstance paramHazelcastInstance, Properties paramProperties, String paramString) {
}
@Override
public void destroy() {
}
理由はわかりませんが、DB からオブジェクトを削除しているときにコードが失敗します。考えられる解決策はありますか?