項目中遇到的問題,項目遇到問題

來源:互聯網
上載者:User

項目中遇到的問題,項目遇到問題

一、Spring 事務問題

1.描述:service1 中的 a 調用 b,b 調用了 service2 中的 c ,c 調用了 service3 中的 d

期望:d 拋出異常時(我真實項目中拋出的是 Sql 異常),d,c 復原,而 a,b 不復原。

測試:考慮到 Spring 事務的自調用和 cglib 動態代理下的 spring 事務配置。添加了 <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>。

Demo:

自訂異常:

public class MyException extends SQLException { private static final long serialVersionUID = 1L; public MyException() { super(); } public MyException(String reason, String sqlState, int vendorCode, Throwable cause) { super(reason, sqlState, vendorCode, cause); } public MyException(String reason, String SQLState, int vendorCode) { super(reason, SQLState, vendorCode); } public MyException(String reason, String sqlState, Throwable cause) { super(reason, sqlState, cause); } public MyException(String reason, String SQLState) { super(reason, SQLState); } public MyException(String reason, Throwable cause) { super(reason, cause); } public MyException(String reason) { super(reason); } public MyException(Throwable cause) { super(cause); } }MyException.java

Dao:

@Repositorypublic class TxDao { @Autowired private JdbcTemplate jdbcTemplate; public void updateA() { String sql = "update tx_test set a_field = 1 where id = 1"; jdbcTemplate.update(sql); } public void updateB() { String sql = "update tx_test set b_field = 1 where id = 1"; jdbcTemplate.update(sql); } public void updateC() { String sql = "update tx_test set c_field = 1 where id = 1"; jdbcTemplate.update(sql); } public void updateD() { String sql = "update tx_test set d_field = 1 where id = 1"; jdbcTemplate.update(sql); }}TxDao.java

ABService:

@Servicepublic class ABService { @Autowired private TxDao txDao; @Autowired private CService cService; @Transactional public void aMethod() throws MyException { System.out.println("aMethod"); txDao.updateA(); ((ABService) AopContext.currentProxy()).bMetod(); } @Transactional public void bMetod() throws MyException { System.out.println("bMethod"); txDao.updateB(); cService.cMethod(); } }ABService.java

CService:

@Servicepublic class CService { @Autowired private TxDao txDao; @Autowired private DService dService; @Transactional(rollbackFor=MyException.class) public void cMethod() throws MyException { System.out.println("cMethod..."); txDao.updateC(); dService.dMethod(); }}CService.java

DService:

@Servicepublic class DService { @Autowired private TxDao txDao; @Transactional(rollbackFor=MyException.class) public void dMethod() throws MyException{ System.out.println("dMethod..."); txDao.updateD(); throw new MyException(); }}DService.java

測試代碼:

@Testpublic void test() { ABService service = context.getBean(ABService.class); try { service.aMethod(); } catch(MyException e) { e.printStackTrace(); }}View Code

(1)測試 rollbackFor 和 noRollbackFor

過程:

自訂了一個 Sql 異常 MyException 繼承自 SQLException,從 d 拋出,一直向上拋。

對 a, b 的 @Transactional 的 noRollbackFor 屬性設定為 MyException.class,而 c,d 的 rollbackFor 屬性設定為 MyException.class。

控制台輸出:

aMethod
bMethod
cMethod...
dMethod...

資料庫輸出:

測試結果: a,b,c,d 四個方法全部復原。

原因尋找:發現在容器初始化的時候就讀取了所有的 事務方法的 RollBackFor 和  noRollBackFor 屬性定義的 Value 值。

詳細參見:org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(java.lang.Class<?>)

在某個事務方法拋出異常後,整個事務都進行了復原,感覺與配置 noRollBackFor 沒有關係。

詳細參見:org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransaction

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.    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.        retVal = invocation.proceedWithInvocation();    }    catch (Throwable ex) {        // target invocation exception        completeTransactionAfterThrowing(txInfo, ex);        throw ex;    }    finally {        cleanupTransactionInfo(txInfo);    }    commitTransactionAfterReturning(txInfo);    return retVal;}

在標紅的地方會去掉目標方法,如目標方法拋出異常,則會進入到 catch 塊,執行完 catch 塊的代碼繼續向上拋出。catch 塊能捕獲到 MyException。

通過斷點跟蹤發現在 completeTransactionAfterThrowing() 方法裡進行了復原,等復原之後最後去調用了 commitTransactionAfterReturning() 方法。

這樣看來 noRollBackFor 甚至沒有什麼作用,有哪位大神看到這裡並且知道原理的話,請不吝賜教,謝謝。

(2)測試 Spring 事務的傳播行為。

過程:將 c 的傳播行為改為 REQUIRES_NEW,其他還是預設。同時在 c 方法中處理了 d 拋上來的異常。

控制台輸出:

aMethod
bMethod
cMethod...

資料庫輸出:

發現根本就不會去執行 d 方法。這裡就不是很明白了。

上面提供了兩個測試,事實上測試了  n 種方式,都沒有行的通。等以後對 spring 事務理解加深之後再來解析。這裡直接說最終的解決辦法。

解決辦法:

1.手動控制事務

2.服務拆分,比如這裡 a,b 單獨事務,c,d 單獨事務。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.