我想了解Hibernate中的悲觀鎖定機制(通過MySQL數據庫)。休眠悲觀鎖定模式
我試圖運行下面的例子:
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Student student = null;
Student studentTwo = null;
try {
session.beginTransaction();
student = (Student) session.get(Student.class, 1, LockMode.PESSIMISTIC_WRITE);
//I was hoping this line would thrown an error
studentTwo = (Student) session.get(Student.class, 1, LockMode.PESSIMISTIC_WRITE);
System.out.println(student.getName());
System.out.println(studentTwo.getName());
student.setName("John Doe");
session.getTransaction().commit();
session.close();
}catch(HibernateException ex){
}
}
而是讓我一個錯誤,它只是執行罰款。是否有某種我誤解的概念。這種行爲是否正常?
我能夠測試樂觀鎖定非常好,因此對於悲觀鎖定是否存在對概念的一些誤解或者存在某些代碼丟失的情況。
好吧,讓我來試試。非常感謝。 –
是的,嘗試訪問使用兩個會話做了伎倆。非常感謝解釋。 –