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

          評論:
          # re: 哪位兄弟了解"事務(wù)" 幫幫小弟 不勝感謝 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("事務(wù)發(fā)生錯誤,事務(wù)被取消! ");
          }
          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) {};
          }

          }
          }
          }  回復(fù)  更多評論
            
          # re: 哪位兄弟了解"事務(wù)" 幫幫小弟 不勝感謝 2006-08-12 11:39 | #
          精通EJB上的例子
          try{
          /*
          *1:設(shè)置環(huán)境。為訪問JNDI,碧血初始化上下文工廠、提供URL、所需的用戶名、密碼
          *2:具體細(xì)節(jié),可以參考應(yīng)用服務(wù)器的給出的產(chǎn)品文檔
          *3:注意,重點參考文檔中有關(guān)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:執(zhí)行事務(wù)
          */
          userTran.begin();
          //完成業(yè)務(wù)操作
          userTran.commit();
          }
          catch(Exception e)
          //處理異常,包括事務(wù)回滾的處理。

          }  回復(fù)  更多評論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 凉城县| 荥经县| 盐池县| 门头沟区| 罗平县| 长宁区| 彰化市| 辉南县| 南安市| 黔江区| 宾阳县| 台东县| 甘孜县| 洛南县| 雷山县| 台前县| 株洲县| 邢台县| 阆中市| 香河县| 三穗县| 普陀区| 南部县| 沁水县| 柘荣县| 峨眉山市| 吉木萨尔县| 昌宁县| 额济纳旗| 庆元县| 沙洋县| 永善县| 永平县| 静宁县| 寻甸| 普兰县| 塔城市| 怀集县| 哈尔滨市| 仙游县| 治多县|