日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
面試突擊:為什么事務(wù)@Transactional會失效?

導(dǎo)致 @Transactional 失效的常見場景有以下 5 個:

  1. 非 public 修飾的方法。
  2. timeout 超時時間設(shè)置過小。
  3. 代碼中使用 try/catch 處理異常。
  4. 調(diào)用類內(nèi)部的 @Transactional 方法。
  5. 數(shù)據(jù)庫不支持事務(wù)。

很多人只知道答案但不知道原因,這就像只談戀愛不結(jié)婚一樣,是不能讓人接受的,所以本篇我們就來討論一下,導(dǎo)致事務(wù)失效的背后原因到底是啥?

在以上 5 種場景中,第 2 種(timeout 超時時間設(shè)置過?。┖偷?5 種(數(shù)據(jù)庫不支持事務(wù))很好理解,我們這里就不贅述了,本文我們重點來討論其他 3 種情況。

1、非 public 修飾的方法

非 public 修飾的方法上,即使加了 @Transactional 事務(wù)依然不會生效,原因是因為 @Transactional 使用的是 Spring AOP 實現(xiàn)的,而 Spring AOP 是通過動態(tài)代理實現(xiàn)的,而 @Transactional 在生成代理時會判斷,如果方法為非 public 修飾的方法,則不生成代理對象,這樣也就沒辦法自動執(zhí)行事務(wù)了,它的部分實現(xiàn)源碼如下:

protected TransactionAttribute computeTransactionAttribute(Method method, Class targetClass) {
// Don't allow no-public methods as required.
// 非 public 方法,設(shè)置為 null
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
return null;
}
// 后面代碼省略....
}

2、try/catch 導(dǎo)致事務(wù)失效

@Transactional 執(zhí)行流程是:@Transactional 會在方法執(zhí)行前,會自動開啟事務(wù);在方法成功執(zhí)行完,會自動提交事務(wù);如果方法在執(zhí)行期間,出現(xiàn)了異常,那么它會自動回滾事務(wù)。

然而如果在方法中自行添加了 try/catch 之后,事務(wù)就不會自動回滾了,這是怎么回事呢?

造成這個問題的主要原因和 @Transactional 注解的實現(xiàn)有關(guān),它的部分實現(xiàn)源碼如下:

protected Object invokeWithinTransaction(Method method, Class targetClass, final InvocationCallback invocation)
throws Throwable {
// If the transaction attribute is null, the method is non-transactional.
final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
final PlatformTransactionManager tm = determineTransactionManager(txAttr);
final String joinpointIdentification = methodIdentification(method, targetClass);

if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
// Standard transaction demarcation with getTransaction and commit/rollback calls.
// 自動開啟事務(wù)
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
Object retVal = null;
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
// 反射調(diào)用業(yè)務(wù)方法
retVal = invocation.proceedWithInvocation();
}
catch (Throwable ex) {
// target invocation exception
// 異常時,在 catch 邏輯中,自動回滾事務(wù)
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
}
finally {
cleanupTransactionInfo(txInfo);
}
// 自動提交事務(wù)
commitTransactionAfterReturning(txInfo);
return retVal;
}
else {
// .....
}
}

從上述實現(xiàn)源碼我們可以看出:當(dāng)執(zhí)行的方法中出現(xiàn)了異常,@Transactional 才能感知到,然后再執(zhí)行事務(wù)回滾,而當(dāng)開發(fā)者自行添加了 try/catch 之后,@Transactional 就感知不到異常了,從而就不會觸發(fā)事務(wù)的自動回滾了,這就是為什么當(dāng) @Transactional 遇到 try/catch 之后就不會自動回滾(事務(wù))的原因。

3、調(diào)用類內(nèi)用的 @Transactional 方法

當(dāng)調(diào)用類內(nèi)部的 @Transactional 修飾的方法時,事務(wù)也不會生效,如下代碼所示:

@RequestMapping("/save")
public int saveMappping(UserInfo userInfo) {
return save(userInfo);
}
@Transactional
public int save(UserInfo userInfo) {
// 非空效驗
if (userInfo == null ||
!StringUtils.hasLength(userInfo.getUsername()) ||
!StringUtils.hasLength(userInfo.getPassword()))
return 0;
int result = userService.save(userInfo);
int num = 10 / 0; // 此處設(shè)置一個異常
return result;
}

上述代碼在添加用戶之后即使遇到了異常,程序也沒有執(zhí)行回滾,這是因為 @Transactional 是基于 Spring AOP 實現(xiàn)的,而 Spring AOP 又是基于動態(tài)代理實現(xiàn)的,而當(dāng)調(diào)用類內(nèi)部的方法時,不是通過代理對象完成的,而是通過 this 對象實現(xiàn)的,這樣就繞過了代理對象,從而事務(wù)就失效了。

總結(jié)

非 public 修飾的方法在 @Transactional 實現(xiàn)時做了判斷,如果是非 public 則不會生成代理對象,所以事務(wù)就失效了;而調(diào)用類內(nèi)部的 @Transactional 修飾的方法時,也是因為沒有成功調(diào)用代理對象,是通過 this 來調(diào)用方法的,所以事務(wù)也失效了;@Transactional 在遇到開發(fā)者自定義的 try/catch 也會失效,這是因為 @Transactional 只有感知到了異常才會自動回滾(事務(wù)),但如果用戶自定義了 try/catch,那么 @Transactional 就感知不到異常,所以也就不會自動回滾事務(wù)了。


文章名稱:面試突擊:為什么事務(wù)@Transactional會失效?
網(wǎng)頁鏈接:http://www.5511xx.com/article/cddcgjd.html