@Controller@EnableAutoConfigurationpublicclassSampleController{@AutowiredprivateWeiboRepositoryweiboRepository;@AutowiredprivateTwitterRepositorytwitterRepository;@RequestMapping("/postWeibo")@ResponseBodypublicvoidpostWeibo(){Weiboweibo=newWeibo("Weibo");weiboRepository.save(weibo);postTwitter();}@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)publicvoidpostTwitter(){Twittertwitter=newTwitter("Twitter");twitterRepository.save(twitter);thrownewRuntimeException();}publicstaticvoidmain(String[]args)throwsException{SpringApplication.run(SampleController.class,args);}}代码逻辑:添加一条微博的同时添加一条推特,postTwitter手动抛出异常模拟回滚预期结果:Twitter表由于抛出异常造成回滚什么都没有添加,但Weibo表正常添加实际结果:虽抛出异常但Twitter、Weibo表均正常添加请问我已经设置了@Transactional,事务级别为REQUIRED表明postTwitter会单独开启新的事务,为何实际执行结果与预期不符合呢?
相关分类