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);
}
阅读 83 · 发布于 2023-02-24
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
GoJS 给右键菜单添加外边框阅读 995
-
软考-系统架构设计师:系统性能设计-阿姆达尔解决方案阅读 3719
-
HTTP Content-Type 为 application/x-www-form-urlencoded 时注意事项阅读 2756
-
Spring Cloud OpenFeign 自定义 ErrorDecoder阅读 428
-
VisualVM 2.0.6 最大堆内存大小问题阅读 1227
-
Linux 恢复删除的文件阅读 4869
-
PostgreSQL 统计数组中的元素个数阅读 5607
-
Android MediaMetadataRetriever 获取多媒体文件信息阅读 3875
-
Java jcmd 命令操作 JFR 飞行记录仪阅读 1575
-
Windows 网络适配器显示:网络管理员已经禁用了 Internet 连接共享阅读 1825