posts - 11,  comments - 28,  trackbacks - 0
          本文參考了
          iBatis DAO入門與進階(http://www.matrix.org.cn/resource/article/44/44058_iBatis+DAO.html
          ?iBatis DAO事務探索(http://www.aygfsteel.com/RongHao/archive/2006/01/20/28817.html

          今天繼續研究了JPetStore的持久層,其中由于看了一篇文章的誤導,導致我對其中的事務處理深表懷疑。通過閱讀源代碼與看上面兩篇文章,對這個問題才認識清楚。和我當初預想的一致。
          第一、持久層的研究
          iBatis Dao的處理流程首先得到daoManager,它是通過文件配置的方式
          配置文件為com.ibatis.jpetstore.persistence.dao.xml
          <transactionManager?type="SQLMAP">
          ??????
          <property?name="SqlMapConfigResource"
          ????????value
          ="com/ibatis/jpetstore/persistence/sqlmapdao/sql/sql-map-config.xml"/>
          ????
          </transactionManager>

          ????
          <dao?interface="com.ibatis.jpetstore.persistence.iface.ItemDao"
          ??????implementation
          ="com.ibatis.jpetstore.persistence.sqlmapdao.ItemSqlMapDao"/>
          其中,具體的數據庫源的配置采用另外一個配置文件sql-map-config.xml,其余部分就是描述了各個實現dao接口的類(下文會具體的描述)。
          這個配置文件將被com.ibatis.jpetstore.persistence.DaoConfig.java 調用得到系統的daoManager,整個過程也就是一個工廠模式。得到了daoManger,通過它來統一管理對數據庫的操作。

          為了使用ibatis的DAO對數據庫進行操作,
          首先是定義了一批接口類,位于包:com.ibatis.jpetstore.persistence.iface 。然后是一批實現這個接口類,位于包:com.ibatis.jpetstore.persistence.sqlmapdao
          這些所有實現類繼承與sqlMapDaoTemplate,如下:
          public?class?ItemSqlMapDao?extends?BaseSqlMapDao?implements?ItemDao?{

          ??
          public?ItemSqlMapDao(DaoManager?daoManager)?{
          ????
          super(daoManager);
          ??}


          }

          public?class?BaseSqlMapDao?extends?SqlMapDaoTemplate?{

          ??
          protected?static?final?int?PAGE_SIZE?=?4;

          ??
          public?BaseSqlMapDao(DaoManager?daoManager)?{
          ????
          super(daoManager);
          ??}


          }
          sqlMapDaoTemplate是ibatis的一個核心類了,由它最終完成對數據庫的訪問。


          最后在業務邏輯層com.ibatis.jpetstore.service調用持久層完成對數據庫的訪問。

          public?class?OrderService?{

          ??
          private?DaoManager?daoManager;
          ??

          ??
          public?OrderService()?{
          ????daoManager?
          =?DaoConfig.getDaoManager();
          ????itemDao?
          =?(ItemDao)?daoManager.getDao(ItemDao.class);
          ????sequenceDao?
          =?(SequenceDao)?daoManager.getDao(SequenceDao.class);
          ????orderDao?
          =?(OrderDao)?daoManager.getDao(OrderDao.class);
          ??}

          }
          上面代碼即是,業務邏輯層根據dao.xml中指定的接口類型(ItemDao)得到它的實現類(ItemSqlMapDao),即是根據dao.xm中以下配置得到具體的dao實現類
          <dao?interface="com.ibatis.jpetstore.persistence.iface.ItemDao"
          ??????implementation
          ="com.ibatis.jpetstore.persistence.sqlmapdao.ItemSqlMapDao"/>


          然后通過這些實現類進行數據庫訪問操作。如在OrderService下:

          public?void?insertOrder(Order?order)?{
          ????
          try?{
          ??????
          //?Get?the?next?id?within?a?separate?transaction
          ??????order.setOrderId(getNextId("ordernum"));

          ??????daoManager.startTransaction();

          ??????itemDao.updateAllQuantitiesFromOrder(order);
          ??????orderDao.insertOrder(order);

          ??????daoManager.commitTransaction();
          ????}
          ?finally?{
          ??????daoManager.endTransaction();
          ????}

          ??}

          其中還值得注意的是,在各個實現類中ItemSqlMapDao并不包含對事務的處理,所有對于事務的處理都是在業務邏輯層調用。這樣做的好處在于所有Dao的操作都是原子操作,方便進行各種業務邏輯的組裝。(以前,我設計時候,這個地方沒有設計好)

          第二、事務處理的研究
          此處涉及到了事務的處理,來分析一下事務處理的過程。
          首先看一個事務處理的時序圖2005_12_18_224916_OkWdiCStgH.gif

          從這張時序圖可以看到事務的處理是由代理類DataProxy完成的,查看DataProxy的代碼

          ?public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)
          ??????
          throws?Throwable?{
          ????Object?result?
          =?null;
          ????
          if?(PASSTHROUGH_METHODS.contains(method.getName()))?{
          ??????
          try?{
          ????????result?
          =?method.invoke(daoImpl.getDaoInstance(),?args);
          ??????}
          ?catch?(Throwable?t)?{
          ????????
          throw?ClassInfo.unwrapThrowable(t);
          ??????}

          ????}
          ?else?{
          ??????StandardDaoManager?daoManager?
          =?daoImpl.getDaoManager();
          ??????DaoContext?context?
          =?daoImpl.getDaoContext();

          ??????
          if?(daoManager.isExplicitTransaction())?{
          ????????
          //?Just?start?the?transaction?(explicit)
          ????????try?{
          ??????????context.startTransaction();
          ??????????result?
          =?method.invoke(daoImpl.getDaoInstance(),?args);
          ????????}
          ?catch?(Throwable?t)?{
          ??????????
          throw?ClassInfo.unwrapThrowable(t);
          ????????}

          ??????}
          ?else?{
          ????????
          //?Start,?commit?and?end?the?transaction?(autocommit)
          ????????try?{
          ??????????context.startTransaction();
          ??????????result?
          =?method.invoke(daoImpl.getDaoInstance(),?args);
          ??????????context.commitTransaction();
          ????????}
          ?catch?(Throwable?t)?{
          ??????????
          throw?ClassInfo.unwrapThrowable(t);
          ????????}
          ?finally?{
          ??????????context.endTransaction();
          ????????}

          ??????}


          ????}

          ????
          return?result;
          ??}

          根據這段代碼應該很清楚的知道Dao框架采用AOP模式,截獲所調用的方法,并檢查事務處理是否已經顯式的開始執行,如果沒有,它將調用事務管理器中的startTransaction()創建一個新的事務處理調用,然后執行被截獲的方法,然后commitTransaction。如果有,則直接執行所調用的方法。
          所以,在此Dao框架下,所有沒有顯式的通過startTransaction調用事務的方法,都是一個獨立的事務。如果要想在一個事務中完成幾次調用,必須自己通過DaoMangaer手動處理事務。

          至此,ibatis的JpetshopStore的持久層運行機制以及ibatis的Dao框架事務處理研究完畢。

          posted on 2007-01-16 21:15 滌生 閱讀(2809) 評論(2)  編輯  收藏


          FeedBack:
          # re: ibaits的JPetStore中持久層的研究、事務的處理
          2008-05-29 16:28 | 王天奇
          好 VERY GOOD  回復  更多評論
            
          # re: ibaits的JPetStore中持久層的研究、事務的處理
          2008-07-26 10:21 | xuxiaolei
          非常不錯  回復  更多評論
            

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


          網站導航:
           
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(5)

          隨筆檔案

          UML

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 奎屯市| 襄汾县| 五大连池市| 涿鹿县| 化隆| 扎兰屯市| 兴业县| 东港市| 武穴市| 鄄城县| 资源县| 电白县| 上饶市| 望城县| 寿宁县| 鸡西市| 岢岚县| 洱源县| 宁乡县| 宝应县| 五台县| 迭部县| 灌南县| 洛浦县| 宁化县| 安多县| 清徐县| 石门县| 沿河| 南丰县| 营山县| 麻阳| 集安市| 赞皇县| 微山县| 牙克石市| 辽阳县| 海兴县| 叶城县| 互助| 蒲江县|