1. 程式人生 > >Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理

Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理

public rollback long tar jpa oid config 新的 autowire

文章目錄

  1. 1. 聲明式事務
  2. 2. Spring Boot默認集成事務
  3. 3. 實戰演練4. 源代碼
    1. 3.1. 實體對象
    2. 3.2. DAO 相關
    3. 3.3. Service 相關
    4. 3.4. 測試,測試

本文講解 Spring Boot 如何使用聲明式事務管理。

聲明式事務

Spring 支持聲明式事務,使用 @Transactional 註解在方法上表明這個方法需要事務支持。此時,Spring 攔截器會在這個方法調用時,開啟一個新的事務,當方法運行結束且無異常的情況下,提交這個事務。

Spring 提供一個 @EnableTransactionManagement 註解在配置類上來開啟聲明式事務的支持。使用了 @EnableTransactionManagement 後,Spring 會自動掃描註解 @Transactional 的方法和類。

Spring Boot默認集成事務

Spring Boot 默認集成事務,所以無須手動開啟使用 @EnableTransactionManagement 註解,就可以用 @Transactional註解進行事務管理。我們如果使用到 spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa,Spring Boot 會自動默認分別註入
DataSourceTransactionManager 或 JpaTransactionManager。

實戰演練

我們在前文「Spring Boot 揭秘與實戰(二) 數據存儲篇 - MySQL」的案例上,進行實戰演練。

實體對象

我們先創建一個實體對象。為了便於測試,我們對外提供一個構造方法。

  1. public class Author {
  2. private Long id;
  3. private String realName;
  4. private String nickName;
  5. public Author() {}
  6. public Author(String realName, String nickName) {
  7. this.realName = realName;
  8. this.nickName = nickName;
  9. }
  10. // SET和GET方法
  11. }

DAO 相關

這裏,為了測試事務,我們只提供一個方法新增方法。

  1. @Repository("transactional.authorDao")
  2. public class AuthorDao {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. public int add(Author author) {
  6. return jdbcTemplate.update("insert into t_author(real_name, nick_name) values(?, ?)",
  7. author.getRealName(), author.getNickName());
  8. }
  9. }

Service 相關

我們提供三個方法。通過定義 Author 的 realName 字段長度必須小於等於 5,如果字段長度超過規定長度就會觸發參數異常。

值得註意的是,noRollbackFor 修飾表明不做事務回滾,rollbackFor 修飾的表明需要事務回滾。

  1. @Service("transactional.authorService")
  2. public class AuthorService {
  3. @Autowired
  4. private AuthorDao authorDao;
  5. public int add1(Author author) {
  6. int n = this.authorDao.add(author);
  7. if(author.getRealName().length() > 5){
  8. throw new IllegalArgumentException("author real name is too long.");
  9. }
  10. return n;
  11. }
  12. @Transactional(noRollbackFor={IllegalArgumentException.class})
  13. public int add2(Author author) {
  14. int n = this.authorDao.add(author);
  15. if(author.getRealName().length() > 5){
  16. throw new IllegalArgumentException("author real name is too long.");
  17. }
  18. return n;
  19. }
  20. @Transactional(rollbackFor={IllegalArgumentException.class})
  21. public int add3(Author author) {
  22. int n = this.authorDao.add(author);
  23. if(author.getRealName().length() > 5){
  24. throw new IllegalArgumentException("author real name is too long.");
  25. }
  26. return n;
  27. }
  28. }

測試,測試

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(WebMain.class)
  3. public class TransactionalTest {
  4. @Autowired
  5. protected AuthorService authorService;
  6. [email protected]
  7. public void add1() throws Exception {
  8. authorService.add1(new Author("梁桂釗", "梁桂釗"));
  9. authorService.add1(new Author("LiangGzone", "LiangGzone"));
  10. }
  11. [email protected]
  12. public void add2() throws Exception {
  13. authorService.add2(new Author("梁桂釗", "梁桂釗"));
  14. authorService.add2(new Author("LiangGzone", "LiangGzone"));
  15. }
  16. @Test
  17. public void add3() throws Exception {
  18. authorService.add3(new Author("梁桂釗", "梁桂釗"));
  19. authorService.add3(new Author("LiangGzone", "LiangGzone"));
  20. }
  21. }

我們分別對上面的三個方法進行測試,只有最後一個方法進行了事務回滾。

源代碼

相關示例完整代碼: springboot-action

(完)



技術分享
  • 版權聲明:本文由 梁桂釗 發表於 梁桂釗的博客
  • 轉載聲明:自由轉載-非商用-非衍生-保持署名(創意共享3.0許可證),非商業轉載請註明作者及出處,商業轉載請聯系作者本人。
  • 文章標題:Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理
  • 文章鏈接:http://blog.720ui.com/2017/springboot_02_data_transactional/

Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理