如鵬網 大學生計算機學習社區

          CowNew開源團隊

          http://www.cownew.com 郵件請聯系 about521 at 163.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            363 隨筆 :: 2 文章 :: 808 評論 :: 0 Trackbacks

          Spring 事務管理創造性的解決了很多以前要用重量級的應用服務器才能解決的事務問題,那么其實現原理一定很深奧吧?可是如果讀者仔細研究了Spring事務管理的代碼以后就會發現,事務管理其實也是如此簡單的事情。這也印證了在本書開頭的一句話“重劍無鋒、大巧不工”,Spring并沒有使用什么特殊的API,它運行的原理就是事務的原理。下面是DataSourceTransactionManager的啟動事務用的代碼(經簡化):
          protected void doBegin(Object transaction, TransactionDefinition definition)
          {
          ?DataSourceTransactionObject txObject =
          (DataSourceTransactionObject) transaction;
          ?Connection con = null;
          ?try
          ?{
          ??if (txObject.getConnectionHolder() == null)
          ??{
          ???Connection newCon = this.dataSource.getConnection();
          ???txObject.setConnectionHolder(
          new ConnectionHolder(newCon), true);
          ??}
          ??txObject.getConnectionHolder()
          .setSynchronizedWithTransaction(true);
          ??con = txObject.getConnectionHolder().getConnection();

          ??Integer previousIsolationLevel = DataSourceUtils
          ?????.prepareConnectionForTransaction(con, definition);
          ??txObject.setPreviousIsolationLevel(previousIsolationLevel);
          ??if (con.getAutoCommit())
          ??{
          ???txObject.setMustRestoreAutoCommit(true);
          ???con.setAutoCommit(false);
          ??}
          ??txObject.getConnectionHolder().setTransactionActive(true);
          ??// Bind the session holder to the thread.
          ??if (txObject.isNewConnectionHolder())
          ??{
          ???TransactionSynchronizationManager.bindResource(
          getDataSource(),txObject.getConnectionHolder());
          ??}
          ?}
          ?catch (SQLException ex)
          ?{
          ??DataSourceUtils.releaseConnection(con, this.dataSource);
          ??throw new CannotCreateTransactionException(
          ?????"Could not open JDBC Connection for transaction", ex);
          ?}
          }
          本文出自:http://www.cownew.com
          在調用一個需要事務的組件的時候,管理器首先判斷當前調用(即當前線程)有沒有一個事務,如果沒有事務則啟動一個事務,并把事務與當前線程綁定。Spring使用TransactionSynchronizationManager的bindResource方法將當前線程與一個事務綁定,采用的方式就是ThreadLocal,這可以從TransactionSynchronizationManager類的代碼看出。
          public abstract class TransactionSynchronizationManager
          {
          ?……
          ?private static final ThreadLocal currentTransactionName = new ThreadLocal();
          ?private static final ThreadLocal currentTransactionReadOnly = new ThreadLocal();
          ?private static final ThreadLocal actualTransactionActive = new ThreadLocal();?……
          }
          從doBegin的代碼中可以看到在啟動事務的時候,如果Connection是的自動提交的(也就是getAutoCommit()方法返回true)則事務管理就會失效,所以首先要調用setAutoCommit(false)方法將其改為非自動提交的。setAutoCommit(false)這個動作在有的JDBC驅動中會非常耗時,所以最好在配置數據源的時候就將“autoCommit”屬性配置為true。

          posted on 2007-01-02 23:33 CowNew開源團隊 閱讀(3238) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 出国| 峡江县| 紫金县| 图们市| 那曲县| 安塞县| 濉溪县| 应用必备| 昭觉县| 延吉市| 肇州县| 沙坪坝区| 依兰县| 昌乐县| 昭平县| 会泽县| 垫江县| 奉新县| 舟山市| 扬州市| 女性| 泌阳县| 南平市| 柳河县| 温宿县| 剑河县| 荣成市| 普宁市| 肇州县| 东兰县| 霍州市| 仁寿县| 嵊州市| 蕲春县| 明星| 瑞金市| 凤山县| 岗巴县| 铁力市| 宝清县| 南平市|