自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring聲明式事務在哪些情況下會失效?

開發(fā) 架構
在Spring中事務管理的方式有兩種,編程式事務和聲明式事務。先詳細介紹一下兩種事務的實現(xiàn)方式

[[393241]]

編程式事務

在Spring中事務管理的方式有兩種,編程式事務和聲明式事務。先詳細介紹一下兩種事務的實現(xiàn)方式.

配置類

  1. @Configuration 
  2. @EnableTransactionManagement 
  3. @ComponentScan("com.javashitang"
  4. public class AppConfig { 
  5.  
  6.     @Bean 
  7.     public DruidDataSource dataSource() { 
  8.         DruidDataSource ds = new DruidDataSource(); 
  9.         ds.setDriverClassName("com.mysql.jdbc.Driver"); 
  10.         ds.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true"); 
  11.         ds.setUsername("test"); 
  12.         ds.setPassword("test"); 
  13.         ds.setInitialSize(5); 
  14.         return ds; 
  15.     } 
  16.  
  17.     @Bean 
  18.     public DataSourceTransactionManager dataSourceTransactionManager() { 
  19.         return new DataSourceTransactionManager(dataSource()); 
  20.     } 
  21.  
  22.     @Bean 
  23.     public JdbcTemplate jdbcTemplate(DataSource dataSource) { 
  24.         return new JdbcTemplate(dataSource); 
  25.     } 
  26.  
  27.     @Bean 
  28.     public TransactionTemplate transactionTemplate() { 
  29.         return new TransactionTemplate(dataSourceTransactionManager()); 
  30.     } 
  1. public interface UserService { 
  2.     void addUser(String name, String location); 
  3.     default void doAdd(String name) {}; 
  1. @Service 
  2. public class UserServiceV1Impl implements UserService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.     @Autowired 
  7.     private TransactionTemplate transactionTemplate; 
  8.  
  9.     @Override 
  10.     public void addUser(String name, String location) { 
  11.         transactionTemplate.execute(new TransactionCallbackWithoutResult() { 
  12.  
  13.             @Override 
  14.             protected void doInTransactionWithoutResult(TransactionStatus status) { 
  15.                 try { 
  16.                     String sql = "insert into user (`name`) values (?)"
  17.                     jdbcTemplate.update(sql, new Object[]{name}); 
  18.                     throw new RuntimeException("保存用戶信息失敗"); 
  19.                 } catch (Exception e) { 
  20.                     e.printStackTrace(); 
  21.                     status.setRollbackOnly(); 
  22.                 } 
  23.             } 
  24.         }); 
  25.     } 

可以看到編程式事務的方式并不優(yōu)雅,因為業(yè)務代碼和事務代碼耦合到一塊,當發(fā)生異常的時候還得需要手動回滾事務(比使用JDBC方便多類,JDBC得先關閉自動自動提交,然后根據(jù)情況手動提交或者回滾事務)

如果讓你優(yōu)化事務方法的執(zhí)行?你會如何做?

「其實我們完全可以用AOP來優(yōu)化這種代碼,設置好切點,當方法執(zhí)行成功時提交事務,當方法發(fā)生異常時回滾事務,這就是聲明式事務的實現(xiàn)原理」

使用AOP后,當我們調(diào)用事務方法時,會調(diào)用到生成的代理對象,代理對象中加入了事務提交和回滾的邏輯。

聲明式事務

Spring aop動態(tài)代理的方式有如下幾種方法

JDK動態(tài)代理實現(xiàn)(基于接口)(JdkDynamicAopProxy)

CGLIB動態(tài)代理實現(xiàn)(動態(tài)生成子類的方式)(CglibAopProxy)

AspectJ適配實現(xiàn)

spring aop默認只會使用JDK和CGLIB來生成代理對象

@Transactional可以用在哪里?

@Transactional可以用在類,方法,接口上

用在類上,該類的所有public方法都具有事務

用在方法上,方法具有事務。當類和方法同時配置事務的時候,方法的屬性會覆蓋類的屬性

用在接口上,一般不建議這樣使用,因為只有基于接口的代理會生效,如果Spring AOP使用cglib來實現(xiàn)動態(tài)代理,會導致事務失效(因為注解不能被繼承)

@Transactional失效的場景

@Transactional注解應用到非public方法(除非特殊配置,例如使用AspectJ 靜態(tài)織入實現(xiàn) AOP)

自調(diào)用,因為@Transactional是基于動態(tài)代理實現(xiàn)的

異常在代碼中被你自己try catch了

異常類型不正確,默認只支持RuntimeException和Error,不支持檢查異常

事務傳播配置不符合業(yè)務邏輯

@Transactional注解應用到非public方法

「為什么只有public方法上的@Transactional注解才會生效?」

首相JDK動態(tài)代理肯定只能是public,因為接口的權限修飾符只能是public。cglib代理的方式是可以代理protected方法的(private不行哈,子類訪問不了父類的private方法)如果支持protected,可能會造成當切換代理的實現(xiàn)方式時表現(xiàn)不同,增大出現(xiàn)bug的可能醒,所以統(tǒng)一一下。

「如果想讓非public方法也生效,你可以考慮使用AspectJ」

自調(diào)用,因為@Transactional是基于動態(tài)代理實現(xiàn)的

當自調(diào)用時,方法執(zhí)行不會經(jīng)過代理對象,所以會導致事務失效。例如通過如下方式調(diào)用addUser方法時,事務會失效

  1. // 事務失效 
  2. @Service 
  3. public class UserServiceV2Impl implements UserService { 
  4.  
  5.     @Autowired 
  6.     private JdbcTemplate jdbcTemplate; 
  7.  
  8.     @Override 
  9.     public void addUser(String name, String location) { 
  10.         doAdd(name); 
  11.     } 
  12.  
  13.     @Transactional 
  14.     public void doAdd(String name) { 
  15.         String sql = "insert into user (`name`) values (?)"
  16.         jdbcTemplate.update(sql, new Object[]{name}); 
  17.         throw new RuntimeException("保存用戶失敗"); 
  18.     } 

可以通過如下方式解決

  1. @Autowired注入自己,假如為self,然后通過self調(diào)用方法
  2. @Autowired ApplicationContext,從ApplicationContext通過getBean獲取自己,然后再調(diào)用
  1. // 事務生效 
  2. @Service 
  3. public class UserServiceV2Impl implements UserService { 
  4.  
  5.     @Autowired 
  6.     private JdbcTemplate jdbcTemplate; 
  7.     @Autowired 
  8.     private UserService userService; 
  9.  
  10.     @Override 
  11.     public void addUser(String name, String location) { 
  12.         userService.doAdd(name); 
  13.     } 
  14.  
  15.     @Override 
  16.     @Transactional 
  17.     public void doAdd(String name) { 
  18.         String sql = "insert into user (`name`) values (?)"
  19.         jdbcTemplate.update(sql, new Object[]{name}); 
  20.         throw new RuntimeException("保存用戶失敗"); 
  21.     } 

異常在代碼中被你自己try catch了

這個邏輯從源碼理解比較清晰,只有當執(zhí)行事務拋出異常才能進入completeTransactionAfterThrowing方法,這個方法里面有回滾的邏輯,如果事務方法都沒拋出異常就只會正常提交

  1. // org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransaction 
  2.  
  3. try { 
  4.   // This is an around advice: Invoke the next interceptor in the chain. 
  5.   // This will normally result in a target object being invoked. 
  6.   // 執(zhí)行事務方法 
  7.   retVal = invocation.proceedWithInvocation(); 
  8. catch (Throwable ex) { 
  9.   // target invocation exception 
  10.   completeTransactionAfterThrowing(txInfo, ex); 
  11.   throw ex; 
  12. finally { 
  13.   cleanupTransactionInfo(txInfo); 

異常類型不正確,默認只支持RuntimeException和Error,不支持檢查異常

異常體系圖如下。當拋出檢查異常時,spring事務不會回滾。如果拋出任何異常都回滾,可以配置rollbackFor為Exception

  1. @Transactional(rollbackFor = Exception.class) 

事務傳播配置不符合業(yè)務邏輯

假如說有這樣一個場景,用戶注冊,依次保存用戶基本信息到user表中,用戶住址信息到地址表中,當保存用戶住址信息失敗時,我們也要保證用戶信息注冊成功。

  1. public interface LocationService { 
  2.     void addLocation(String location); 
  1. @Service 
  2. public class LocationServiceImpl implements LocationService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.  
  7.     @Override 
  8.     @Transactional 
  9.     public void addLocation(String location) { 
  10.         String sql = "insert into location (`name`) values (?)"
  11.         jdbcTemplate.update(sql, new Object[]{location}); 
  12.         throw new RuntimeException("保存地址異常"); 
  13.     } 
  1. @Service 
  2. public class UserServiceV3Impl implements UserService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.     @Autowired 
  7.     private LocationService locationService; 
  8.  
  9.     @Override 
  10.     @Transactional 
  11.     public void addUser(String name, String location) { 
  12.         String sql = "insert into user (`name`) values (?)"
  13.         jdbcTemplate.update(sql, new Object[]{name}); 
  14.         locationService.addLocation(location); 
  15.     } 

調(diào)用發(fā)現(xiàn)user表和location表都沒有插入數(shù)據(jù),并不符合我們期望,你可能會說拋出異常了,事務當然回滾了。好,我們把調(diào)用locationService的部分加上try catch

  1. @Service 
  2. public class UserServiceV3Impl implements UserService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.     @Autowired 
  7.     private LocationService locationService; 
  8.  
  9.     @Override 
  10.     @Transactional 
  11.     public void addUser(String name, String location) { 
  12.         String sql = "insert into user (`name`) values (?)"
  13.         jdbcTemplate.update(sql, new Object[]{name}); 
  14.         try { 
  15.             locationService.addLocation(location); 
  16.         } catch (Exception e) { 
  17.             e.printStackTrace(); 
  18.         } 
  19.     } 

調(diào)用發(fā)現(xiàn)user表和location表還是都沒有插入數(shù)據(jù)。這是因為在LocationServiceImpl中事務已經(jīng)被標記成回滾了,所以最終事務還會回滾。

要想最終解決就不得不提到Spring的事務傳播行為了,不清楚的小伙伴看《面試官:Spring事務的傳播行為有幾種?》

Transactional的事務傳播行為默認為Propagation.REQUIRED?!溉绻斍按嬖谑聞?,則加入該事務。如果當前沒有事務,則創(chuàng)建一個新的事務」

此時我們把LocationServiceImpl中Transactional的事務傳播行為改成Propagation.REQUIRES_NEW即可

「創(chuàng)建一個新事務,如果當前存在事務,則把當前事務掛起」

所以最終的解決代碼如下

  1. @Service 
  2. public class UserServiceV3Impl implements UserService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.     @Autowired 
  7.     private LocationService locationService; 
  8.  
  9.     @Override 
  10.     @Transactional 
  11.     public void addUser(String name, String location) { 
  12.         String sql = "insert into user (`name`) values (?)"
  13.         jdbcTemplate.update(sql, new Object[]{name}); 
  14.         try { 
  15.             locationService.addLocation(location); 
  16.         } catch (Exception e) { 
  17.             e.printStackTrace(); 
  18.         } 
  19.     } 
  20. @Service 
  21. public class LocationServiceImpl implements LocationService { 
  22.  
  23.     @Autowired 
  24.     private JdbcTemplate jdbcTemplate; 
  25.  
  26.     @Override 
  27.     @Transactional(propagation = Propagation.REQUIRES_NEW) 
  28.     public void addLocation(String location) { 
  29.         String sql = "insert into location (`name`) values (?)"
  30.         jdbcTemplate.update(sql, new Object[]{location}); 
  31.         throw new RuntimeException("保存地址異常"); 
  32.     } 
  1. @Service 
  2. public class LocationServiceImpl implements LocationService { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.  
  7.     @Override 
  8.     @Transactional(propagation = Propagation.REQUIRES_NEW) 
  9.     public void addLocation(String location) { 
  10.         String sql = "insert into location (`name`) values (?)"
  11.         jdbcTemplate.update(sql, new Object[]{location}); 
  12.         throw new RuntimeException("保存地址異常"); 
  13.     } 

 

責任編輯:武曉燕 來源: Java識堂
相關推薦

2024-11-29 11:01:25

2021-12-13 11:12:41

Spring事務失效

2009-06-22 09:01:57

Spring聲明式事務

2022-04-13 20:53:15

Spring事務管理

2024-11-13 19:03:14

2022-12-06 10:39:43

Spring事務失效

2021-09-06 13:42:14

Spring聲明式事務

2022-09-14 19:50:22

事務場景流程

2022-09-22 09:57:20

Spring事務失效

2009-02-11 13:08:29

事務提交事務管理Spring

2009-02-11 11:14:31

事務管理事務開始Spring

2024-08-01 08:29:45

Spring參數(shù)類型

2022-02-14 16:53:57

Spring項目數(shù)據(jù)庫

2023-05-26 07:19:49

Spring聲明式事務

2009-06-22 11:01:12

2023-10-26 07:32:42

2022-07-05 14:19:30

Spring接口CGLIB

2023-07-05 08:45:18

Spring事務失效場景

2021-11-08 15:17:15

變量Defer 失效

2025-01-16 08:45:48

點贊
收藏

51CTO技術棧公眾號