简体   繁体   English

@Transactional 不能从预定的方法工作

[英]@Transactional not working from scheduled method

I have two classess as below.我有两个类如下。

@Component
@RequiredArgsConstructor
public class SomeScheduler {
    
    private final SomeService someService;

    @Scheduled( ... )
    void doScheduledJob() {
         someService.doJob();
    }
}
@Service
@RequiredArgsConstructor
@Transactional
public class SomeService {
    
    private final SomeRepository someRepository;

    public void doJob() {
        someRepository.findByCustomized();
        
        ...
    }
}

The problem is that SomeService is working without @Transactional .问题是SomeService没有@Transactional的情况下工作。

With configuration in application.yml logging.level.org.springframework.transactional.interceptor: DEBUG , I found that there are some logs like No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findByCustomized]: This method is not transactional.通过 application.yml logging.level.org.springframework.transactional.interceptor: DEBUG中的配置,我发现有一些日志,例如No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findByCustomized]: This method is not transactional.

What I wanna know is:我想知道的是:

  1. There is no transctional on service method.没有事务性的服务方法。 Why?为什么? (If I change with MANDATORY propagation, it throws exception) (如果我用 MANDATORY 传播改变,它会抛出异常)
  2. my repository method which has @Query annotation works.我的具有@Query注释的存储库方法有效。 I've thought that 'JPA requires transaction' but it wasn't.我曾认为“JPA 需要交易”,但事实并非如此。 Why?为什么?

I've searched this for a while, but couldn't understand.我已经搜索了一段时间,但无法理解。 Can any one explain this or show me any article about?任何人都可以解释这一点或向我展示任何有关的文章吗?

Thank you.谢谢你。

looks like "findByCustomized" is a non-modifiable query method (aka select), there is no need in transaction thus看起来“findByCustomized”是一种不可修改的查询方法(又名选择),因此在事务中不需要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring:@Transactional @Scheduled方法抛出TransactionException - Spring : @Transactional @Scheduled method throws TransactionException @Transactional在控制器方法上不起作用 - @Transactional on controller method not working 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working Spring @Transactional persist 方法不起作用 - Spring @Transactional persist method not working 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method 在Spring中是否可以从非事务方法中调用事务方法? - Is it possible to invoke transactional method from non transactional method in Spring? EntityManager 持久化和合并在@Transactional 方法中不起作用 - EntityManager persist and merge not working in @Transactional method 如何从事务方法调用非事务方法 - How to call non-transactional methods from a Transactional method 当来自另一个事务的延迟加载的 Hibernate 对象被传递给方法时,Spring @Transactional 不起作用 - Spring @Transactional not working when Hibernate object with lazy loading coming from another transaction is passed down to method 从事务方法开始新事务 - Starting a new transaction from transactional method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM