2

我正在将 Hazelcast 与 Mysql 和 ofc hibernate 用于连接。我有一个多线程应用程序,它正在处理数据,然后尝试从 hazelcast 和 db 中删除。

但过了一段时间,我得到

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

当应用程序在处理后尝试从 db 中删除一些数据时,就会发生这种情况。以下是此异常的详细错误日志:


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

这就是道:


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() {

    }

我不知道为什么,但是从数据库中删除对象时代码失败了。任何可能的解决方案?

4

0 回答 0