我想了解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){
}
}
而是让我一个错误,它只是执行罚款。是否有某种我误解的概念。这种行为是否正常?
我能够测试乐观锁定非常好,因此对于悲观锁定是否存在对概念的一些误解或者存在某些代码丢失的情况。
好吧,让我来试试。非常感谢。 –
是的,尝试访问使用两个会话做了伎俩。非常感谢解释。 –