gpt4 book ai didi

spring-boot - Spring Boot和Spring Data JPA中的事务管理

转载 作者:行者123 更新时间:2023-12-04 21:55:50 25 4
gpt4 key购买 nike

充分的事务管理工作Spring Data Repository创建了自己的事务并挂起了事件的事务。

我有以下Spring应用程序:

应用类别:

@SpringBootApplication
@EnableTransactionManagement
public class SpringbootTxApplication {... }

服务等级:
@Service
public class EntityService {
...
public void addEntityWithoutTransaction(MyEntity myEntity) {
log.debug("addEntityWithoutTransaction start");
myEntityRepository.save(myEntity);
log.debug("addEntityWithoutTransaction end");
}

@Transactional
public void addEntityTransaction(MyEntity myEntity) {
log.debug("addEntityTransaction start");
myEntityRepository.save(myEntity);
log.debug("addEntityTransaction end");
}
}

当执行我的执行每个方法一次的EntityServiceTest并将spring事务日志记录在跟踪中时,我得到以下输出:
... TRACE ... o.s.t.i.TransactionInterceptor           : Getting transaction for [de.miwoe.service.EntityService.addEntityTransaction]
... DEBUG ... de.miwoe.service.EntityService : addEntityTransaction start
... TRACE ... o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... TRACE ... o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... DEBUG ... de.miwoe.service.EntityService : addEntityTransaction end
... TRACE ... o.s.t.i.TransactionInterceptor : Completing transaction for [de.miwoe.service.EntityService.addEntityTransaction]


... DEBUG ... de.miwoe.service.EntityService           : addEntityWithoutTransaction start
... TRACE ... o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... TRACE ... o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... DEBUG ... de.miwoe.service.EntityService : addEntityWithoutTransaction end

显然,根据日志,@ Transactional-Annotation在addEntityTransaction中起作用,但是存储库仍会创建自己的事务。

为什么?官方文档( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions)描述了它不应该开始一个新的,如果它已经存在的话。

(有时,约定优于配置似乎更像是对约定优于配置的激怒...。)

我想念什么吗?

(完整的代码也可以在这里找到: https://github.com/miwoe/springboot-tx)

最佳答案

您的问题(要点):

... TRACE ... o.s.t.i.TransactionInterceptor           : Getting transaction for [de.miwoe.service.EntityService.addEntityTransaction]
... DEBUG ... de.miwoe.service.EntityService : addEntityTransaction start
... TRACE ... o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... TRACE ... o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
... DEBUG ... de.miwoe.service.EntityService : addEntityTransaction end
... TRACE ... o.s.t.i.TransactionInterceptor : Completing transaction for [de.miwoe.service.EntityService.addEntityTransaction]

Obviously according to the log, the @Transactional-Annotation is working in addEntityTransaction, but the repository still creates its own transaction.



回答:
您从事一项实物交易。

当启动调用服务时spring启动新事务并设置 TransactionStatus.isNewTransaction = true,当您
调用dao method spring检查该方法是否也是事务性的,并为dao创建第二个事务,但为第二个事务 TransactionStatus.isNewTransaction = false设置BUT。如果仅在这种情况下为dao方法/类设置required_new,则在提交时仅将其标记为 TransactionStatus.isNewTransaction = true. )已提交。如果标记了第二个事务,则它将在提交时被忽略,并且第一个事务已提交。
AbstractPlatformTransactionManager

if (status.isNewTransaction()) {
if (status.isDebug()) {
logger.debug("Initiating transaction commit");
}
doCommit(status);
}

您可以在 Debug模式下检查。

要点:您在一个可能标记为提交或回滚的事务中工作。在TRACE中,您可以看到有关 Spring 事务做什么的详细信息,对您而言,在物理事务中创建多少逻辑事务无关紧要。对于具有传播级别REQUIRED的事务,您有保证,如果从另一事务方法中调用事务方法,则仅创建一个物理事务,并且提交或回滚一个物理事务。

PROPAGATION_REQUIRED

When the propagation setting is PROPAGATION_REQUIRED, a logical transaction scope is created for each method upon which the setting is applied. Each such logical transaction scope can determine rollback-only status individually, with an outer transaction scope being logically independent from the inner transaction scope. Of course, in case of standard PROPAGATION_REQUIRED behavior, all these scopes will be mapped to the same physical transaction. So a rollback-only marker set in the inner transaction scope does affect the outer transaction’s chance to actually commit (as you would expect it to).

关于spring-boot - Spring Boot和Spring Data JPA中的事务管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com