我有一个关于 Spring boot 的项目,我尝试使用 @Transactional 和协程,但出现错误
org.springframework.dao.InvalidDataAccessApiUsageException:执行更新/删除查询;嵌套异常是 javax.persistence.TransactionRequiredException。
现在可以使用@Transactional 和协程吗?
override suspend fun invoke() {
val result = withContext(Dispatchers.IO) { deactivate() }
}
@Transactional
private suspend fun deactivate(): Int {
//data base call 1
//data base call 2
// ...
}
最佳答案
在 @Transactional
方法调用期间,您不能启动在不同线程上执行事务性工作的协程。
@Transactional
导致代码被拦截。 deactivate()
前后,交易相关的代码会被调用。
因为协程可以在不同的线程上启动,所以它们将在这些事务调用之外启动。
相关 StackOverflow 主题:Spring - @Transactional - What happens in background?
关于spring - 是否可以在 spring boot 中使用 @Transactional 和 kotlin 协程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64136645/