【问题标题】:Spring data JPA lockingSpring数据JPA锁定
【发布时间】:2017-11-13 19:30:18
【问题描述】:

我需要在我的实现中使用@Lock:

@Lock(LockModeType.PESSIMISTIC_WRITE)
private Note findOneForUpdate(BigInteger id) {
    return noteDao.findOne(id);
}

但其他消息来源说它应该在接口中:

@Repository
public interface NoteRepository extends JpaRepository<Note, BigInteger>, NoteDao {
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    Note findOne(BigInteger id);
}

那么,第一个选项可能吗?我用 spring-boot-starter-data-jpa 1.5.3.RELEASE 试过了,但是锁不起作用。

【问题讨论】:

  • @Lock 在 Spring Data JPA 存储库之外的任何东西上都不会做任何事情。此注释由 Spring Data JPA 读取,仅此而已。

标签: spring hibernate spring-boot transactions spring-data-jpa


【解决方案1】:

仓库类中需要@Lock注解

@Lock(LockModeType.PESSIMISTIC_WRITE) // not required
private Note findOneForUpdate(BigInteger id) {
    return noteDao.findOne(id);
}

@Repository
public interface NoteRepository extends JpaRepository<Note, BigInteger>, NoteDao {
    @Lock(LockModeType.PESSIMISTIC_WRITE) // required
    Note findOne(BigInteger id);
}

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2017-11-19
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多