隨筆-11  評論-5  文章-1  trackbacks-0
          哪位兄弟了解"事務" 幫幫小弟? 不勝感謝webdn客戶服務
          posted on 2006-08-02 18:55 Crespo 閱讀(369) 評論(2)  編輯  收藏

          評論:
          # re: 哪位兄弟了解"事務" 幫幫小弟 不勝感謝 2006-08-07 19:14 | Crespo
          自己先跟一帖
          import javax.transaction.*;
          import java.sql.*;
          import java.util.*;
          import java.io.*;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          public class JTATest
          {
          public static void main(String args[])
          {
          Context ctx=null;
          Hashtable ht=new Hashtable();
          DataSource ds=null;
          Statement stmt=null;
          ResultSet rs=null;
          Connection myConn = null;
          UserTransaction tx = null;
          String tablename="student";
          String str1="INSERT INTO student VALUES ('01001001','ixucheng', 33)";
          String str2="INSERT INTO student VALUES ('01001002','hangsan', 44)";
          try
          {
          ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
          ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
          ctx=new InitialContext(ht);
          String str="javax.transaction.UserTransaction";
          tx =(UserTransaction)ctx.lookup(str);
          tx.begin();
          ds = (javax.sql.DataSource)ctx.lookup("TestData");
          myConn= ds.getConnection();
          stmt = myConn.createStatement();
          stmt.executeUpdate (str1);
          stmt.executeUpdate (str2);
          tx.commit();
          System.out.println("Success!");
          }
          catch (Exception E)
          {
          try{tx.rollback();}catch(Exception e){}
          System.out.println("事務發生錯誤,事務被取消! ");
          }
          finally
          {
          if (rs != null)
          {
          try{ rs.close(); } catch (Exception ignore) {};
          }
          if (stmt != null)
          {
          try{ stmt.close(); } catch (Exception ignore) {};
          }
          if (myConn != null)
          {
          try { myConn.close(); } catch (Exception ignore) {};
          }

          }
          }
          }  回復  更多評論
            
          # re: 哪位兄弟了解"事務" 幫幫小弟 不勝感謝 2006-08-12 11:39 | #
          精通EJB上的例子
          try{
          /*
          *1:設置環境。為訪問JNDI,碧血初始化上下文工廠、提供URL、所需的用戶名、密碼
          *2:具體細節,可以參考應用服務器的給出的產品文檔
          *3:注意,重點參考文檔中有關JNDI的部分
          */
          java.util.Properties env = ...
          /*
          *2:獲得JNDI初始上下文
          */
          Context ctx = new InitialContext(env);
          /*
          *3:借助于JNDI,查找JTA UserTransaction接口
          *通常,容器都回江JTA暴露在“java:comp/UserTransaction”位置

          *
          */
          userTran = (javax.transaction.UserTransaction)
          ctx.lookup("java:comp/UserTransaction");
          /*
          *4:執行事務
          */
          userTran.begin();
          //完成業務操作
          userTran.commit();
          }
          catch(Exception e)
          //處理異常,包括事務回滾的處理。

          }  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 绥宁县| 五寨县| 阳新县| 固镇县| 延寿县| 祁门县| 富顺县| 武乡县| 公安县| 三都| 焉耆| 赫章县| 城市| 大名县| 梓潼县| 常宁市| 永城市| 进贤县| 五常市| 昭觉县| 盘山县| 上蔡县| 辽中县| 昭通市| 镇宁| 泰兴市| 西昌市| 东海县| 承德市| 博野县| 延津县| 临沭县| 揭东县| 玛多县| 沙雅县| 泾川县| 三穗县| 晴隆县| 临潭县| 威海市| 海安县|