參考:https://www.cnblogs.com/kesimin/p/9546225.html
一:啓動類加入@EnableTransactionManagement註解(注意springboot(=默認加了,親測2.1.6版本不加也行)
@SpringBootApplication
@EnableTransactionManagement //開啓事務
public class MkBlogApplication {
public static void main(String[] args)
{
SpringApplication.run(MkBlogApplication.class, args);
}
}
二: @Transactional註解加在需要事務的方法上,我們在保存到數據庫後,拋出一個異常,結果會回滾
@Override
@Transactional
public int add(ArticleVo articleVo) {
Article article = new Article();
User user = new User();
user.setUserId(articleVo.getUserId());
article.setTitle(articleVo.getTitle());
article.setContent(articleVo.getContent());
article.setImgUrl(articleVo.getImgUrl() == ""? "imgurl":articleVo.getImgUrl());
article.setUser(user);
article.setAddUser(articleVo.getUserId());
article.setAddTime(new Date());
String cateids =articleVo.getCateIds();
if (cateids.length() >0){
String ids=cateids.substring(0,cateids.length()-1);
article.setCateIds(ids);
}
logger.info("cateids",cateids);
articleResposition.save(article);
throw new MkException("測試繼承之RuntimeException的異常");
//return 1;
}
三:接下來,我們測試下事務傳播特性,保存文章的方法裏面我們調用user服務插入一條用戶數據
@Override
@Transactional
public int add(ArticleVo articleVo) {
Article article = new Article();
User user = new User();
user.setUserId(articleVo.getUserId());
article.setTitle(articleVo.getTitle());
article.setContent(articleVo.getContent());
article.setImgUrl(articleVo.getImgUrl() == ""? "imgurl":articleVo.getImgUrl());
article.setUser(user);
article.setAddUser(articleVo.getUserId());
article.setAddTime(new Date());
String cateids =articleVo.getCateIds();
if (cateids.length() >0){
String ids=cateids.substring(0,cateids.length()-1);
article.setCateIds(ids);
}
logger.info("cateids",cateids);
articleResposition.save(article);
User user1 = new User();
user1.setEmail("sdsdsds");
user1.setMobile("13762691578");
user1.setUserName("張三");
userService.save(user1);
//throw new MkException("測試繼承之RuntimeException的異常");
return 1;
}
添加用戶具體代碼如下
@Override
public User save(User user) {
userRepository.save(user);
throw new MkException("測試一番");
//return userRepository.save(user);
}
結果:文章表和用戶表都不會插入記錄
四:接下來我們換一下,保存用戶加入事務,保存文章不要事務
@Override
public int add(ArticleVo articleVo) {
Article article = new Article();
User user = new User();
user.setUserId(articleVo.getUserId());
article.setTitle(articleVo.getTitle());
article.setContent(articleVo.getContent());
article.setImgUrl(articleVo.getImgUrl() == ""? "imgurl":articleVo.getImgUrl());
article.setUser(user);
article.setAddUser(articleVo.getUserId());
article.setAddTime(new Date());
String cateids =articleVo.getCateIds();
if (cateids.length() >0){
String ids=cateids.substring(0,cateids.length()-1);
article.setCateIds(ids);
}
logger.info("cateids",cateids);
articleResposition.save(article);
User user1 = new User();
user1.setEmail("sdsdsds");
user1.setMobile("13762691578");
user1.setUserName("張三");
userService.save(user1);
//throw new MkException("測試繼承之RuntimeException的異常");
return 1;
}
@Override
@Transactional
public User save(User user) {
userRepository.save(user);
throw new MkException("測試一番");
//return userRepository.save(user);
}
結果是文章會插入一條記錄,而user表不會
其他的就不演示了
PROPAGATION_REQUIRED | 如果當前沒有事務,就新建一個事務,如果已經存在一個事務中,加入到這個事務中。這是最常見的選擇。 |
PROPAGATION_SUPPORTS |
支持當前事務,如果當前沒有事務,就以非事務方式執行。 |
PROPAGATION_MANDATORY |
使用當前的事務,如果當前沒有事務,就拋出異常。 |
PROPAGATION_REQUIRES_NEW |
新建事務,如果當前存在事務,把當前事務掛起。 |
PROPAGATION_NOT_SUPPORTED | 以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。 |
PROPAGATION_NEVER | 以非事務方式執行,如果當前存在事務,則拋出異常。 |
PROPAGATION_NESTED | 如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則執行與PROPAGATION_REQUIRED類似的操作。 |