Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

前端 未结 17 1847
别跟我提以往
别跟我提以往 2020-12-02 16:36

I have a java project that runs on a webserver. I always hit this exception.

I read some documentation, and found that pessimistic locking (or optimistic, but I read

相关标签:
17条回答
  • 2020-12-02 16:59

    It doesn't appear that you are actually using the email that you retrieve from the database, but an older copy that you get as a parameter. Whatever is being used for version control on the row has changed between when the previous version was retrieved and when you are doing the update.

    You probably want your code to look more like:

    @Transactional
    Public void test(String id, String subject){
       Email email = getEmailById(id);
       email.setSubject(subject);
       updateEmail(email);
    }
    
    0 讨论(0)
  • 2020-12-02 16:59

    This exception is probably caused by optimistic locking (or by a bug in your code). You're probably using it without knowing. And your pseudo-code (which should be replaced by real code to be able to diagnose the problem) is wrong. Hibernate saves all the modifications done to attached entities automatically. You shouldn't ever call update, merge or saveOrUpdate on an attached entity. Just do

    Email email = session.get(emailId);
    email.setSubject(subject);
    

    No need to call update. Hibernate will flush the changes automatically before committing the transaction.

    0 讨论(0)
  • 2020-12-02 16:59

    Don't set an Id to the object you are saving as the Id will be autogenerated

    0 讨论(0)
  • 2020-12-02 17:02

    I had the this problem on my project.

    After I implemented optimistic locking, I got the same exception. My mistake was that I did not remove the setter of the field that became the @Version. As the setter was being called in java space, the value of the field did not match the one generated by the DB anymore. So basically the version fields did not match anymore. At that point any modification on the entity resulted in:

    org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

    I am using H2 in memory DB and Hibernate.

    0 讨论(0)
  • 2020-12-02 17:02

    I had the experienced the same issue in different context of my project and there are different scenarios like

     - object is accessed from various source like (server side and client)
     - without any interval accessing the same object from a different place
    

    In the first case

    When I issue a server cal, before save the that object their one call from js and trying to save and another place, I got like, js call is going two, three times(I thing that call binding thing cause the issue)

    I solved by

    e.preventDefault()
    

    The second case,

    object.lock()
    
    0 讨论(0)
  • 2020-12-02 17:04

    This error occurred for me when I was trying to update the same row from 2 different sessions. I updated a field in one browser while a second was open and had already stored the original object in its session. When I attempted to update from this second "stale" session I get the stale object error. In order to correct this I refetch my object to be updated from the database before I set the value to be updated, then save it as normal.

    0 讨论(0)
提交回复
热议问题