从两个tomcat实例更新相同的DB时获取StaleStateException(要更新的行是不同的)

时间:2018-02-12 12:32:09

标签: java hibernate stalestateexception

尝试使用来自两个不同的tomcat实例的hibernate来更新表的两个不同的行时获得以下异常。获得的所有来源都提示并发更新问题,但此处仍有两个不同的行正在更新获得异常。查看版本控制问题,因为两个不同的会话试图保持同一个类。

Class有以下字段:

    @Entity
    @Table(name="yt_platinum_user")
    public class PlatinumUserInfo {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="id_platinum_user")
        private Long idPlatinumUser;

        @Column(name="first_name")
        private String first_name;

        @Column(name="last_name")
        private String last_name;

        @Column(name="email")
        private String email;

        @Column(name="phone")
        private String phone;

        @Column(name="title")
        private String title;

        @Column(name="link_rewrite")
        private String link_rewrite;

        @Column(name="is_fresco_updated")
        private int is_fresco_updated;

        @Column(name="is_infinity_updated")
        private int is_infinity_updated;

        @Column(name="is_mo_updated")
        private int is_mo_updated;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="insert_date")
            private Date insertDateTime=new Date();

        @Version
        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="update_date")
            private Date updateDateTime=new Date();
}

更新DAO类的功能:

public ResponseEntity<Object> updateData(String title,String first_name,String last_name,String link_rewrite)
    {
        try
        {
            List<PlatinumUserInfo> ptInfoList=hibernateTemplate.find("from PlatinumUserInfo where link_rewrite =? ", link_rewrite);
            if(!(ptInfoList==null||ptInfoList.isEmpty()))
            {

                ptInfoList.get(0).setFirst_name(first_name);
                ptInfoList.get(0).setLast_name(last_name);
                ptInfoList.get(0).setTitle(title);
                hibernateTemplate.update(ptInfoList.get(0));
                ResponseEntity<Object> resp=new ResponseEntity<Object>(ptInfoList.get(0), HttpStatus.OK);
                return resp;
            }
            else
            {
                logger.error("No applicable record found for ciphertext :"+link_rewrite);
                return new ResponseEntity<Object>(new CustomMessage("No applicable record found ! "), HttpStatus.OK);
            }
        }
        catch(Exception e)
        {
            logger.error("Exception occurred while getting response :"+e);
            e.printStackTrace();

            return new ResponseEntity<Object>(new CustomMessage("Error occurred while getting response for "+link_rewrite), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

日志:

ERROR threadPoolExecutor-4 org.hibernate.event.def.AbstractFlushingEventListener Could not synchronize database state with session 
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.yatra.product.fresco.model.PlatinumUserInfo#599563]
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1792)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2435)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:705)
    at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:701)
    at com.yatra.product.fresco.dao.PlatinumUserInfoDAO.updateData(PlatinumUserInfoDAO.java:150)
    at com.yatra.product.fresco.service.executor.tasks.PlatinumUserInfoTask.saveDetails(PlatinumUserInfoTask.java:33)
    at com.yatra.product.fresco.service.executor.tasks.PlatinumUserInfoTask.frescoCall(PlatinumUserInfoTask.java:28)
    at com.yatra.product.fresco.service.executor.tasks.PlatinumUserInfoTask.frescoCall(PlatinumUserInfoTask.java:8)
    at com.yatra.product.fresco.util.FrescoCallable.myCall(FrescoCallable.java:21)
    at com.yatra.platform.commons.YatraCallable.call(YatraCallable.java:12)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
2018-02-12 14:18:08 ERROR threadPoolExecutor-4 com.yatra.product.fresco.dao.PlatinumUserInfoDAO Exception occurred while getting response :org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.yatra.product.fresco.model.PlatinumUserInfo] with identifier [599563]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.yatra.product.fresco.model.PlatinumUserInfo#599563] 

0 个答案:

没有答案