2012-01-09 37 views
0
public class BusinessService { //spring bean 

    public dumpAllData(List){ 

    /* Complicated DB operation here 
    * We dont want to be in transaction now (because of performance issues) 
    */ 

    for(...){   //iterating through whole list 
     **updateItem(item);** 
    } 

    } 

    public updateItem(Entity e){ 
    //saves entity into DB 
    //we want to be in transaction now 
    } 

} 

Spring配置:彈簧+休眠,嵌套NOT_SUPPORTED事務屬性

<tx:advice id="txAdvice" transaction-manager="wsTransactionManager"> 
    <tx:attributes>   
     <tx:method name="dumpAllData" propagation="NOT_SUPPORTED" /> 
     <tx:method name="updateItem" propagation="REQUIRES_NEW" /> 
    </tx:attributes> 
</tx:advice> 

可以具有嵌套REQUIRED_NEW傳播將從方法與傳播被調用NOT_SUPPORTED?

事情是我們在dumpAllData()中運行一個廣泛的數據庫操作(〜100Mb),所以我們不想在事務中(oterwise這將是性能問題)。但是我們想要在updateItem方法中進行事務(回滾/提交)(我們只是簡單地更新實體)。

回答

0

我看不出在交易中是如何進行交易的,或者沒有發生交易。你是否評估過性能差異,還是隻是猜測?

無論如何,如果你真的需要做到這一點,那麼updateItem方法應該是在其他的Spring bean,注入BusinessService豆。

事實上,Spring只能在通過代理調用bean方法時啓動/提交事務。如果你從同一個bean的另一個方法調用一個bean方法,Spring不能攔截這個調用並進行事務管理。

+1

在單個事務中完成的大規模數據庫更新可能會給數據庫帶來沉重的負擔(在oracle的情況下會有巨大的重做日誌)。最好在自動提交模式下執行它們,或者分成更小的塊。 – mrembisz 2012-01-09 14:44:05

+0

Autocommit?與休眠?真的不好主意,恕我直言。見http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#batch – 2012-01-09 14:52:44

+0

@Mrembisz:「分成小塊」 - 是的,這是我們的想法。我們希望一個接一個地做出小小的改變。你有任何有用的例子/鏈接? Thx – 2012-01-09 15:02:53

0

如果從同一類的某些方法中調用,則更新方法中的事務註釋將不會被Spring事務基礎結構攔截。有關Spring交易如何運作的更多信息,請參閱Spring Transaction