关闭

WebLogic WorkShop中的数据库操作事务 - JTA

955人阅读 评论(0) 收藏 举报

此代码或许还有不正确的地方,不是最佳方案,但是能够满足事务的提交和回滚.

workshop中新建class文件(java),名字为jta.java,代码如下:

  1. package mypackage;
  2.  
  3. import java.io.Serializable;
  4. import java.sql.*;
  5. import javax.sql.*;
  6. import javax.naming.Context;
  7. import javax.naming.InitialContext;
  8. import javax.transaction.SystemException;
  9. import javax.transaction.UserTransaction;
  10. public class jta implements Serializable
  11. {
  12.     public Context ctx;
  13.     public UserTransaction tx;
  14.     public DataSource ds;
  15.     public Connection myConn;
  16.     public Statement stmt;
  17.     public String v_sqlstr;
  18.  
  19.     public void exeJTASQL()
  20.     {
  21.         int i;
  22.         String l_sqlstr[];
  23.  
  24.         if ( v_sqlstr.trim().length() == 0 )
  25.         {
  26.             System.out.println("参数错误");
  27.         }
  28.  
  29.         l_sqlstr = v_sqlstr.split(";");
  30.         try
  31.         {
  32.             ctx = new InitialContext();
  33.             tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");
  34.             tx.begin();
  35.             ds = (DataSource)ctx.lookup("CMSDS");
  36.             myConn = ds.getConnection();
  37.             stmt = myConn.createStatement();
  38.             for ( i=0; i<l_sqlstr.length; i++ )
  39.             {
  40.                 if ( l_sqlstr[i].startsWith("insert") == true ||
  41.                      l_sqlstr[i].startsWith("update") == true ||
  42.                      l_sqlstr[i].startsWith("delete") == true )
  43.                 {
  44.                     System.out.println(i + ":" + l_sqlstr[i]);
  45.                     stmt.executeUpdate(l_sqlstr[i].toString().toString());
  46.                 }
  47.             }
  48.           
  49.             tx.commit();
  50.             System.out.println("数据提交");
  51.             stmt.close();
  52.             System.out.println("关闭stmt");
  53.         }
  54.         catch ( Exception e)
  55.         {
  56.             System.out.println("执行出错 message:" + e);  
  57.             try
  58.             {
  59.                 tx.rollback();
  60.                 System.out.println("事务回滚");
  61.             }
  62.             catch(SystemException se)
  63.             {
  64.                 System.out.println("回滚错误 message:" + se);
  65.             }
  66.         }
  67.         finally
  68.         {
  69.             try
  70.             {
  71.                 if ( myConn != null )
  72.                 {
  73.                     if ( !myConn.isClosed() )
  74.                     {
  75.                         myConn.close();
  76.                         System.out.println("关闭myConn");
  77.                     }
  78.                 }
  79.             }
  80.             catch(SQLException sqle)
  81.             {
  82.                 ;
  83.             }
  84.         }
  85.     }
  86.  
  87.     public void setV_sqlstr(String v_sqlstr)
  88.     {
  89.         this.v_sqlstr = v_sqlstr;
  90.     }
  91. }

在workshop中的jpf程序的Action中使用:

  1. import mypackage.jta;
  2. /**
  3.  * @jpf:action
  4.  * @jpf:forward name="success" path="newPage1.jsp"
  5.  */ 
  6. protected Forward newAction1() throws Exception
  7. {
  8.         String sqlstr;
  9.    
  10.         jta jtatest;               /*创建类jta的一个实例jtatest*/
  11.         jtatest = new jta();       /*初始化jtatest*/
  12.         /*组装sqlstr语句,注意每个语句结束的";"符号,语句必须是insert,update,delete开头*/
  13.         sqlstr = "insert into table values('aaaa',11,222,'aaaaaaa');";
  14.         sqlstr = sqlstr + "update table1 set name='高级管理员';";
  15.         sqlstr = sqlstr + "insert into table3 values('111',802,100);";
  16.         System.out.println("sqlstr:" + sqlstr);
  17.         jtatest.setV_sqlstr(sqlstr); /*将这段sql语句赋值给实例jtatest的成员变量*/
  18.         jtatest.exeJTASQL();         /*调用方法*/
  19.         return new Forward("success");
  20. }
0
0