Loading...

          java .net

          學習Spring的事務管理(一)

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

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

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

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

          //用代碼控制實現事務管理其中的實現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);//事務狀態

                  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;

              }

           

          }

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

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

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

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


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


          網站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

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

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苍山县| 雷山县| 香格里拉县| 洱源县| 开平市| 军事| 青田县| 台南县| 双城市| 涞源县| 临江市| 宁阳县| 玉林市| 科技| 佳木斯市| 河源市| 内江市| 沙坪坝区| 进贤县| 克山县| 松原市| 玉田县| 柳林县| 淳化县| 安顺市| 上犹县| 龙胜| 云南省| 株洲市| 静乐县| 丰原市| 康定县| 当涂县| 灵台县| 上杭县| 会宁县| 鄂托克旗| 遂溪县| 酉阳| 枣强县| 榆树市|