第1步 在一个线程中,我在hibernate会话中从DB中获取了id为1的Employee并关闭了会话
第2步 我开始了第2阶段的会议,上传了id 1的员工姓名。
第3步 现在回到线程1,我在分离的实例中更改了员工姓名。然后打开hibernate会话 并在db
中更新了emp我是expectind并发更新异常,因为在版本之间已经被线程2更新了。为什么呢 是不是被扔了?我想我在这里错过了一些概念,但不确定是什么?
以下是相关代码: -
主题1
Step 1
Employee Employee1=(Employee)session.get(Employee.class, 1);
session.close();
Step 2
// Start new Thread i.e Thread 2 and update Employee in between
Makes sure thread 2 is done before proceeding to step3
Step 3
Employee.setName("EmployeeUpdated");
session = factory.openSession();
tx = session.beginTransaction();
session.update(Employee);
tx.commit();// expect the concurrent update here
session.close();
主题2
Thread(SessionFactory factory){
this.factory = factory; // same factory as used in Thread1
}
public void run() {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Employee Employee1=(Employee)session.get(Employee.class, 1);
tx.commit();
session.close();
session = factory.openSession();
tx = session.beginTransaction();
Employee1.setName("EmployeeUpdatedByThread2");
session.merge(Employee1);
tx.commit();
session.close();
}
hibernate是否需要explict配置才能在更新时检查版本?