java - JPA(Hibernate) 实现行锁
巴扎黑
巴扎黑 2017-04-18 09:50:52
0
2
350

有1个需求,比如有1列,存在数据库里面,会被多个人并发的修改。
我目前是直接加锁,在锁里面读取这列的某一行,然后执行修改的操作,再释放锁。
但是这样效率太低了,相当于写了一堆Sychnorized方法(或者表锁),别人只能等待锁释放才能操作。
但是,不同的人操作不同的行,应该并行的进行。
请问有什么方式可以实现

巴扎黑
巴扎黑

reply all (2)
Peter_Zhu
  1. Pessimistic lock, that is, if one person accesses this line, other people's access will be blocked. See the LockMode class for details

  2. Optimistic locking, everyone can access it at the same time. Only one person will succeed in the end. Others can complete it by repeatedly retrying the transaction. This is usually achieved by adding a field version to the table. Please refer to here for details

    巴扎黑

    Don’t think about solving this problem at the JVM level, because it will just stop in a cluster environment.

    You can refer to the optimistic lock design of the database, which has been discussed before.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!