Loading...

          java .net

          學習Spring的事務管理(一)

          哈哈,有人在我的blog上留言說也在學習spring,能不能傳個例子共同學習一下spring的事物管理,我也是學了個一知半解,剛剛研究了研究。將其中的一點自己的理解寫下來,共同學習。

          Spring的事物管理有兩種,一種是代碼控制事物,一種是聲明控制實現(xiàn)事物管理(也就是xml配置),咱先來說用代碼控制實現(xiàn)事物管理,這種方式又分為兩類,一種是實現(xiàn)PlatformTransactionManager接口的方法

          查了一下幫助文檔,實現(xiàn)了PlatformTransactionManager接口的有好多類,下面的例子代碼是用的其中的DataSourceTransactionManager.

          這是模擬用戶注冊的一個例子,下面的類實現(xiàn)于自定義接口UserLoginInterface(spring提倡的面向)該類里面定義了兩個屬性,一個是Springjdbc模版類,一個是事務管理器,并都提供了set方法,這兩個對象將在Ioc中自動初始化加載。其中有一個叫UserInfoVO的值對象類,用來傳遞用戶注冊信息

          //用代碼控制實現(xiàn)事務管理其中的實現(xiàn)PlatformTransactionManager接口的方法

          import java.util.*;

          import org.springframework.context.*;

          import org.springframework.jdbc.core.*;

          import org.springframework.transaction.*;

          import org.springframework.jdbc.datasource.DataSourceTransactionManager;

          import org.springframework.transaction.support.DefaultTransactionDefinition;

          public class UserLoginImple implements UserLoginInterface

          {

                 //Spring jdbc模版類

              private JdbcTemplate jdbcTemplate;  

              //事務管理器 PlatformTransactionManager是一個接口

              private PlatformTransactionManager transactionManager


           

              //jdbc模版類對象的set方法

              public void setJdbcTemplate(JdbcTemplate jdbcTemplate)

              {

                  this.jdbcTemplate = jdbcTemplate;

              }

             

              //事務管理器的set方法

              public void setTransactionManager(PlatformTransactionManager transactionManager)

              {

                  this.transactionManager = transactionManager;

              }

             

              //插入用戶信息的方法

              public boolean doUpdateUserInfo(UserInfoVO oneUserInfo)

              {

              //定義兩個sql插入語句

                  String sql = "update userinfo set userPassword=? where userName=?";

                  String sql2 = "update userinfo1 set userPassword=? where userName=?";

           

                  DefaultTransactionDefinition dtf = new                                DefaultTransactionDefinition();//事務定義

                  TransactionStatus status = this.transactionManager.getTransaction(dtf);//事務狀態(tài)

                  try

                  {

                      this.jdbcTemplate.update(sql, new Object[]

                                               {oneUserInfo.getUserName(),

                                               oneUserInfo.getUserPassword()});

                      this.jdbcTemplate.update(sql2, new Object[]

                                               {oneUserInfo.getUserName(),

                                               oneUserInfo.getUserPassword()});

                      this.transactionManager.commit(status);

                  }

                  catch(Exception ex)

                  {

                      this.transactionManager.rollback(status);

                      return false;

                  }

              

                 return true;

              }

           

          }

          關鍵看加粗的部分,首先定義一個事務,然后以這個事務為參數(shù)通過事務管理器創(chuàng)建一個事務狀態(tài),開始執(zhí)行sql代碼,最后事務管理器提交事務狀態(tài),完成。其間如果出現(xiàn)異常,事務管理器回滾事務狀態(tài)。

          我理解著,這里面用了設計模式中的一個叫Command命令的模式,首先定義一個執(zhí)行者(事務對象),然后賦予執(zhí)行者使命(事務狀態(tài)),命令執(zhí)行者執(zhí)行其被賦予的使命,如果失敗,命令執(zhí)行者撤銷執(zhí)行的使命。

          不知道理解的對不對,呵呵。

          posted on 2008-08-26 22:06 閱讀(185) 評論(0)  編輯  收藏


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


          網(wǎng)站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網(wǎng)絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 屯昌县| 桐乡市| 柘城县| 客服| 安顺市| 综艺| 福建省| 隆回县| 兴安县| 天津市| 禄丰县| 资源县| 武夷山市| 麻江县| 宜良县| 竹溪县| 壶关县| 石柱| 临颍县| 图片| 泗阳县| 新营市| 万宁市| 两当县| 大洼县| 彩票| 新安县| 民勤县| 阿拉善右旗| 微山县| 小金县| 巨鹿县| 永春县| 扶绥县| 仁寿县| 甘谷县| 子洲县| 离岛区| 云和县| 南安市| 石嘴山市|