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

          評(píng)論:
          # 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ā)生錯(cuò)誤,事務(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ù)  更多評(píng)論
            
          # re: 哪位兄弟了解"事務(wù)" 幫幫小弟 不勝感謝 2006-08-12 11:39 | #
          精通EJB上的例子
          try{
          /*
          *1:設(shè)置環(huán)境。為訪問JNDI,碧血初始化上下文工廠、提供URL、所需的用戶名、密碼
          *2:具體細(xì)節(jié),可以參考應(yīng)用服務(wù)器的給出的產(chǎn)品文檔
          *3:注意,重點(diǎn)參考文檔中有關(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ù)  更多評(píng)論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 永安市| 巧家县| 南平市| 将乐县| 略阳县| 永寿县| 阜宁县| 抚州市| 邛崃市| 沐川县| 昭苏县| 海城市| 娱乐| 区。| 新蔡县| 邵东县| 福清市| 定州市| 道孚县| 延寿县| 喀喇| 富平县| 绥芬河市| 河北省| 灵璧县| 陕西省| 东宁县| 五台县| 黔江区| 墨江| 秦皇岛市| 沾化县| 普定县| 五华县| 荔波县| 资源县| 八宿县| 洛川县| 锦屏县| 赫章县| 伊川县|