Spring Boot JPA @Lock TransactionRequiredException
Spring Boot JPA 大约 953 字错误信息
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
错误代码
使用了@Lock
实现悲观锁:select...for update
。
import javax.persistence.LockModeType;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
public interface UserRepo extends JpaRepository<User, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
User findByIdIs(Long id);
}
原因
需要使用事务。
解决
此处只是演示解决方法,即:添加@Transactional
注解。javax
和spring
包下的@Transactional
都可以。
一般会在调用Repo
的方法的地方标注@Transactional
事务。
public interface UserRepo extends JpaRepository<User, Long> {
@Transactional
@Lock(LockModeType.PESSIMISTIC_WRITE)
User findByIdIs(Long id);
}
阅读 487 · 发布于 2023-02-24
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Windows 子系统初始化报错:参考的对象类型不支持尝试的操作阅读 4822
-
JMeter 提取 JSON 字段用于下一个请求阅读 1554
-
OpenResty 中使用 pgmoon 连接 PostgreSQL阅读 6357
-
软考-系统架构设计师:需求分类-需求获取阅读 2197
-
算法:买卖股票的最佳时机阅读 1510
-
PostgreSQL 数据库角色管理阅读 2418
-
Java 使用 wait 等待会使 synchronized 升级为重量级锁阅读 1978
-
Ali OSS 抛出 NoSuchKey 错误阅读 411
-
MongoDB 批量更新和删除字段阅读 3467
-
Linux grep 抓取多个关键字阅读 2620