Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理
文章目錄
- 1. 聲明式事務
- 2. Spring Boot默認集成事務
- 3. 實戰演練4. 源代碼
- 3.1. 實體對象
- 3.2. DAO 相關
- 3.3. Service 相關
- 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」的案例上,進行實戰演練。
實體對象
我們先創建一個實體對象。為了便於測試,我們對外提供一個構造方法。
- public class Author {
- private Long id;
- private String realName;
- private String nickName;
- public Author() {}
- public Author(String realName, String nickName) {
- this.realName = realName;
- this.nickName = nickName;
- }
- // SET和GET方法
- }
DAO 相關
這裏,為了測試事務,我們只提供一個方法新增方法。
- @Repository("transactional.authorDao")
- public class AuthorDao {
- @Autowired
- private JdbcTemplate jdbcTemplate;
- public int add(Author author) {
- return jdbcTemplate.update("insert into t_author(real_name, nick_name) values(?, ?)",
- author.getRealName(), author.getNickName());
- }
- }
Service 相關
我們提供三個方法。通過定義 Author 的 realName 字段長度必須小於等於 5,如果字段長度超過規定長度就會觸發參數異常。
值得註意的是,noRollbackFor 修飾表明不做事務回滾,rollbackFor 修飾的表明需要事務回滾。
- @Service("transactional.authorService")
- public class AuthorService {
- @Autowired
- private AuthorDao authorDao;
- public int add1(Author author) {
- int n = this.authorDao.add(author);
- if(author.getRealName().length() > 5){
- throw new IllegalArgumentException("author real name is too long.");
- }
- return n;
- }
- @Transactional(noRollbackFor={IllegalArgumentException.class})
- public int add2(Author author) {
- int n = this.authorDao.add(author);
- if(author.getRealName().length() > 5){
- throw new IllegalArgumentException("author real name is too long.");
- }
- return n;
- }
- @Transactional(rollbackFor={IllegalArgumentException.class})
- public int add3(Author author) {
- int n = this.authorDao.add(author);
- if(author.getRealName().length() > 5){
- throw new IllegalArgumentException("author real name is too long.");
- }
- return n;
- }
- }
測試,測試
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(WebMain.class)
- public class TransactionalTest {
- @Autowired
- protected AuthorService authorService;
- [email protected]
- public void add1() throws Exception {
- authorService.add1(new Author("梁桂釗", "梁桂釗"));
- authorService.add1(new Author("LiangGzone", "LiangGzone"));
- }
- [email protected]
- public void add2() throws Exception {
- authorService.add2(new Author("梁桂釗", "梁桂釗"));
- authorService.add2(new Author("LiangGzone", "LiangGzone"));
- }
- @Test
- public void add3() throws Exception {
- authorService.add3(new Author("梁桂釗", "梁桂釗"));
- authorService.add3(new Author("LiangGzone", "LiangGzone"));
- }
- }
我們分別對上面的三個方法進行測試,只有最後一個方法進行了事務回滾。
源代碼
相關示例完整代碼: springboot-action
(完)
如果覺得我的文章對你有幫助,請隨意打賞。
- 版權聲明:本文由 梁桂釗 發表於 梁桂釗的博客
- 轉載聲明:自由轉載-非商用-非衍生-保持署名(創意共享3.0許可證),非商業轉載請註明作者及出處,商業轉載請聯系作者本人。
- 文章標題:Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理
- 文章鏈接:http://blog.720ui.com/2017/springboot_02_data_transactional/
Spring Boot 揭秘與實戰(二) 數據存儲篇 - 聲明式事務管理