3
我在服務層的方法中添加了@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;
}
誰能告訴我原因,謝謝
您可以指定上述類的包裝工作? – 2014-11-25 05:56:17
以及包裝上述方法的類定義。 – 2014-11-25 06:18:39
您的應用程序類不需要'@ EnableJpaRepositories'和'@ EnableTransactionManagement'。 Spring Boot將檢測這些。確保你的服務被Application類的'@ ComponentScan'檢測到。理想情況下,您不需要聲明'AppConfig'應該被'@ ComponentScan'選中。 – 2014-11-25 06:57:44