Spring Boot和Spring Data JPA中的事务管理

米沃

充分的事务管理工作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

y

您的问题(要点):

... 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]

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

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

当start call service spring启动新事务并设置时TransactionStatus.isNewTransaction = true,当调用dao方法spring时,检查该方法是否也可以事务并为dao创建第二个事务,但为第二个事务TransactionStatus.isNewTransaction = false设置。如果仅在这种情况下为dao方法/类设置了required_new,标记为“TransactionStatus.isNewTransaction = true.在提交时”,仅提交第一个事务(物理)。如果标记第二个事务,则它将在提交时被忽略,并且第一个事务已提交。

AbstractPlatformTransactionManager

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

您可以在调试模式下检查。

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

PROPAGATION_REQUIRED

当传播设置为PROPAGATION_REQUIRED时,将为应用该设置的每种方法创建一个逻辑事务作用域。每个这样的逻辑事务作用域可以单独确定仅回滚状态,而外部事务作用域在逻辑上独立于内部事务作用域。当然,在标准PROPAGATION_REQUIRED行为的情况下,所有这些范围都将映射到同一物理事务。因此,内部事务范围中设置的仅回滚标记确实会影响外部事务实际提交的机会(正如您期望的那样)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章