spring boot @controller @transactional 不起作用 [英] spring boot @controller @transactional doesnt work

查看:127
本文介绍了spring boot @controller @transactional 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器类中有 @Transactional 方法如下:主要问题是每个服务调用都根据日志在自己的事务中运行.

I have @Transactional method in controller class as below : Main issue is each service call runs in its own transaction as per the log.

控制器是否忽略了事务功能?

Is the transactional functionality ignored for controllers?

我希望学生记录不会被保存,因为我在使用另一个服务之后抛出异常,但更新仍然发生在数据库中.我什至在配置类上有 @EnableTransactionManagement .你能帮我解决这个问题吗?

I expect the student record not get saved as I am throwing exception after that using another service, but still the update happens in the database. I even have @EnableTransactionManagement on the config class. can you please help me with this.

@RestController        
@RequestMapping("/api")        
public class Resource {        
@Transactional        
@RequestMapping(value="/test", method = RequestMethod.PUT, produces = "application/json")        
public StudentDTO updateRecord(@RequestBody StudentDTO DTO) throws ApplicationException{ 
    studentservice.find(1234); //jparepository.findone() runs in one transaction       
    studentservice.save(dto); //jparepository.save() runs in one transaction         
    testservice.throwException(); // this method throws application exception        
}        

@Configuration        
@ComponentScan(basePackages={"com.student.*"})        
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})        
@EnableConfigurationProperties        
@EnableTransactionManagement        
public class Application {        
}       

下面是日志:

[TRACE] org.springframework.transaction.interceptor.TransactionInterceptor -  Getting transaction for   [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
[TRACE] org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
[TRACE] org.springframework.transaction.interceptor.TransactionInterceptor - Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
[TRACE] org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
[TRACE] org.springframework.transaction.interceptor.TransactionInterceptor -    Completing transaction for [updaterecord] after exception: xx.xx.ApplicationException
[TRACE]   org.springframework.transaction.interceptor.RuleBasedTransactionAttribute -   Applying rules to determine whether transaction should rollback on   xx.xx.ApplicationException
[TRACE]     org.springframework.transaction.interceptor.RuleBasedTransactionAttribute -    Winning rollback rule is: null
[TRACE]   org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - No   relevant rollback rule found: applying default rules 

我正在使用 spring 数据 jpa,studentservice.find(1234) 调用在 1 个事务中运行的 //jparepository.findone() 方法studentservice.save(dto); 调用 //jparepository.save() 在另一个事务中运行,我们可以从上面的日志中看出

I am using spring data jpa, studentservice.find(1234) invokes //jparepository.findone() method which runs in 1 transaction studentservice.save(dto); invokes //jparepository.save() runs in another transaction which we can tell from the above log

推荐答案

很可能更明智的做法是在服务层而不是在控制器中声明您的事务边界.

In all likelihood it is more advisable that your Transaction boundary should be declared at the Service layer instead of in your Controllers.

更详细的解释请看这里:为什么我们不应该制作一个 Spring MVC 控制器 @Transactional?

For a more detailed explanation have a look here: Why we shouldn't make a Spring MVC controller @Transactional?

这篇关于spring boot @controller @transactional 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆