Spring boot @Transactional не работает

Я добавил @Transactional в метод на уровне обслуживания.

@Transactional(readOnly = false)
public void add(UserFollow uf){
    UserFollow db_uf = userFollowRepository.findByUserIdAndFollowUserId(uf.getUserId(), uf.getFollowUserId());
    if(db_uf == null) { 
        userFollowRepository.save(uf);      
        userCountService.followInc(uf.getFollowUserId(), true);
        userCountService.fansInc(uf.getUserId(), true);

        throw new RuntimeException();// throw an Exception
    }
}

userFollowRepository.save (uf); по-прежнему сохранить безопасный , не откатывается ...

Я включаю диспетчер транзакций в приложении.

@Configuration  
@ComponentScan 
@EnableAutoConfiguration  
@EnableJpaRepositories
@EnableTransactionManagement
public class Application {  

    @Bean
    public AppConfig appConfig() {
       return new AppConfig();
    }

    public static void main(String[] args) {  
       SpringApplication.run(Application.class);  
    }  
}  

Я перемещаю @Transactional на уровень управления, он работает, код:

@Transactional
@RequestMapping(value="following", method=RequestMethod.POST)
public MyResponse follow(@RequestBody Map<String, Object> allRequestParams){
    MyResponse response = new MyResponse();

    Integer _userId = (Integer)allRequestParams.get("user_id");
    Integer _followUserId = (Integer)allRequestParams.get("follow_user_id");



    userFollowService.add(_userId, _followUserId); //this will throw an exception, then rollback


    return response;
}

кто-нибудь может сказать мне причину, спасибо!


person wb_james    schedule 25.11.2014    source источник
comment
Можете указать паки класса выше?   -  person Haim Raman    schedule 25.11.2014
comment
И определения классов, которые являются оболочкой для вышеперечисленных методов.   -  person Haim Raman    schedule 25.11.2014
comment
Вам не нужны @EnableJpaRepositories и @EnableTransactionManagement в вашем классе приложения. Spring Boot обнаружит их. Убедитесь, что ваша служба обнаружена @ComponentScan в вашем Application классе. В идеале вам не нужно объявлять AppConfig, так как это должно быть подхвачено @ComponentScan.   -  person M. Deinum    schedule 25.11.2014
comment
Более конкретно, вызов @Transactional add из того же класса или из другого класса?   -  person chrylis -cautiouslyoptimistic-    schedule 25.11.2014


Ответы (1)


Согласно http://spring.io/guides/gs/managing-transactions/

@EnableTransactionManagement активирует функции бесшовных транзакций Spring, что делает функцию @Transactional

поэтому он начал работать после того, как вы добавили @EnableTransactionManagement

person PaintedRed    schedule 05.02.2015