微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 乐观锁定和org.hibernate.StaleObjectStateException:

我只是在尝试乐观锁定.

我有以下课程:

@Entity
public class Student {

    private Integer id;
    private String firstName;
    private String lastName;
    private Integer version; 
@Version
    public Integer getVersion() {
        return version;
    }

//all other getters ommited.
}

现在我正在抓住其中一个学生并尝试同时更新其属性.

Thread t1 = new Thread(new MyRunnable(id));
    Thread t2 = new Thread(new MyRunnable(id));
    t1.start();
    t2.start();

和MyRunnable内部:

public class MyRunnable implements Runnable {
    private Integer id;
    @Override
    public void run() {
        Session session = HibernateUtil.getSessionFactory().openSession();       
        session.beginTransaction();
        Student student = (Student) session.load(Student.class,id);
        student.setFirstName("xxxx");
        session.save(student);
        session.getTransaction().commit();
        System.out.println("Done"); 
    }

    public MyRunnable(Integer id){
        this.id = id;
    }
}

一个事务成功更新对象和第二个事务抛出的情况:

org.hibernate.staleobjectstateexception: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.vanilla.entity.Student#1]

还行吧.

我的问题是:
1)如果我希望第二个事务什么也不做,并且不抛出任何异常,我该怎么办?

2)如果我希望第二个事务覆盖第一个事务更新的数据,我该怎么办?

谢谢.

解决方法

我试着回答你的问题:

>您使用乐观锁定.因此,您希望在版本冲突时抛出OptimisticLockException – 但您可以捕获它并且不执行任何操作.您不能将其关闭一秒(无论这意味着什么)事务,因为您不知道是否会发生版本冲突(这是乐观锁策略的本质:乐观假设是版本冲突不会经常发生)
>如果发生OptimisticLockException,您基本上有两个选项:

>放弃更改(也许刷新当前状态)
>刷新您的实体(实体)的版本并尝试再次提交

如果您有并发更新,问题是如何或谁将决定哪个状态是实际的“正确”(表示最新的).只要保证一致性,我就不在乎了.

原文地址:https://www.jb51.cc/java/120789.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐