【面試必問】| 哪些場景下Spring的事務(wù)會失效?

源?/?? ? ? ??文/?冰河
注:部分內(nèi)容引用自冰河與貓大人出版的 《深入理解分布式事務(wù):原理與實戰(zhàn)》一書。 文章收錄于GitHub和Gitee: GitHub: https://github.com/sunshinelyz/technology-binghe Gitee: https://gitee.com/binghe001/technology-binghe
Spring事務(wù)不生效總覽

數(shù)據(jù)庫不支持事務(wù)
事務(wù)方法未被Spring管理
public?class?ProductService?{
?@Autowired
?private?ProductDao?productDao;
?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}
方法沒有被public修飾
@Service
public?class?ProductService?{
?@Autowired
?private?ProductDao?productDao;
?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?private?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}
同一類中方法調(diào)用
@Service
public?class?OrderService?{
?@Autowired
?private?OrderDao?orderDao;
?@Autowired
?private?ProductDao?productDao;
?public?void?submitOrder(){
??//生成訂單
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//減庫存
??this.updateProductStockCountById(1,?1L);
?}
?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}
未配置事務(wù)管理器
@Bean
public?PlatformTransactionManager?transactionManager(DataSource?dataSource)?{
?return?new?DataSourceTransactionManager(dataSource);
}
方法的事務(wù)傳播類型不支持事務(wù)
@Service
public?class?OrderService?{
?@Autowired
?private?OrderDao?orderDao;
?@Autowired
?private?ProductDao?productDao;
?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?submitOrder(){
??//生成訂單
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//減庫存
??this.updateProductStockCountById(1,?1L);
?}
?@Transactional(propagation?=?Propagation.NOT_SUPPORTED)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}
不正確的捕獲異常
@Service
public?class?OrderService?{
?@Autowired
?private?OrderDao?orderDao;
?@Autowired
?private?ProductDao?productDao;
?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?submitOrder(){
??//生成訂單
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//減庫存
??this.updateProductStockCountById(1,?1L);
?}
?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??try{
???productDao.updateProductStockCountById(stockCount,?id);
???int?i?=?1?/?0;
??}catch(Exception?e){
???logger.error("扣減庫存異常:",?e.getMesaage());
??}
?}
}
錯誤的標注異常類型
@Transactional(propagation?=?Propagation.REQUIRED)
public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
?try{
??productDao.updateProductStockCountById(stockCount,?id);
?}catch(Exception?e){
??logger.error("扣減庫存異常:",?e.getMesaage());
??throw?new?Exception("扣減庫存異常");
?}
}
@Transactional(propagation?=?Propagation.REQUIRED,rollbackFor?=?Exception.class)
END


頂級程序員:topcoding
做最好的程序員社區(qū):Java后端開發(fā)、Python、大數(shù)據(jù)、AI
一鍵三連「分享」、「點贊」和「在看」
評論
圖片
表情
