WEB开发网
开发学院软件开发Java 如何在Weblogic的全局事务执行多线程操作 阅读

如何在Weblogic的全局事务执行多线程操作

 2009-09-22 00:00:00 来源:WEB开发网   
核心提示: 3:因为涉及到多线程,主线程需要决定何时提交或回滚事务,如何在Weblogic的全局事务执行多线程操作(2),这个我们要自己要实现一个线程结果检查的方法(checkCompletion()),下面就是我自己实现的测试代码

3:因为涉及到多线程,主线程需要决定何时提交或回滚事务,这个我们要自己要实现一个线程结果检查的方法(checkCompletion())。

下面就是我自己实现的测试代码,在Weblogic81测试没有问题。

  1 package weblogic.transaction.internal;
  2 
  3 import weblogic.transaction.TxHelper;
  4 import weblogic.transaction.internal.TransactionManagerImpl;
  5 import javax.transaction.Transaction;
  6 import java.util.ArrayList;
  7 
  8 public class DriverTest {
  9 
 10     private static String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory"; 
 11     private static String PROVIDER_URL = "t3://localhost:8001";  
 12     private static String SQL_INSERT = "insert into test values(?)";
 13     private static String ANO_SQL_INSERT = "insert into test1 values(?)";
 14     
 15     public static void main(String args[])
 16     {
 17         DriverTest test = new DriverTest();
 18         test.multiThreadXATest();
 19     }
 20     
 21     private Connection getConnection(String url, String dsName) throws NamingException, SQLException
 22     {
 23         InitialContext ctx = initializeEnv(url);
 24         DataSource ds = (DataSource)ctx.lookup(dsName);
 25         ctx.close();
 26         return ds.getConnection();
 27     }
 28     
 29     private UserTransaction getUserTransaction() throws NamingException, SQLException
 30     {
 31         InitialContext ctx = initializeEnv(null);
 32         return (UserTransaction)ctx.lookup("javax/transaction/UserTransaction");
 33     }
 34     
 35     private InitialContext initializeEnv(String url) throws NamingException
 36     {
 37         Properties prop = new Properties();
 38         if(url == null)
 39             prop.put(Context.PROVIDER_URL, PROVIDER_URL);
 40         else
 41             prop.put(Context.PROVIDER_URL, url);
 42         prop.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
 43         return new InitialContext(prop);
 44     }
 45     
 46     private void executeInsertInPSMT(Connection conn, String sql)
 47     {
 48         PreparedStatement pstmt = null;
 49         try{
 50             pstmt = conn.prepareStatement(sql);
 51             pstmt.setString(1, "data_to_insert");
 52             pstmt.executeUpdate();
 53             pstmt.close();
 54         }catch(SQLException e){
 55                 e.printStackTrace();
 56         }    
 57     }
 58     
 59     public void multiThreadXATest()
 60     {
 61         ArrayList result = new ArrayList();
 62         try{
 63             UserTransaction userTx = getUserTransaction();
 64             userTx.setTransactionTimeout(1000);
 65             userTx.begin();
 66             Transaction tx = TxHelper.getTransaction();
 67             Connection conn = getConnection("t3://localhost:8011", "TestXADS");
 68             if(conn != null) conn.close();
                  SQLThread thread1 = new SQLThread(tx,result,"t3://localhost:8011","TestXADS", SQL_INSERT);
 69             SQLThread thread2 = new SQLThread(tx,result,"t3://localhost:8021","TestXADS_1", ANO_SQL_INSERT);
 70             thread1.start();
 71             thread2.start();
 72             while(result.size() != 2){
 73                 Thread.currentThread().sleep(1);
 74             }
 75             if(checkCompletion(result)){
 76                 userTx.commit();
 77             }
 78             else{
 79                 userTx.rollback();
 80             }
 81         }catch(Exception e){
 82             e.printStackTrace();
 83         }
 84     }
 85     
 86     private boolean checkCompletion(ArrayList result){
 87         boolean toReturn = true;
 88         for(int loop=0; loop<result.size(); loop++){
 89             if((!((String)result.get(loop)).equals("OK"))){
 90                 toReturn = false;
 91                 break;
 92             }
 93         }
 94         return toReturn;
 95     }
 96     
 97     class SQLThread extends weblogic.kernel.AuditableThread {
 98         
 99         private Transaction tx = null;
100         private ArrayList result = null;
101         private String dsName = null;
102         private String url = null;
103         private String sql = null;
104         
105         public SQLThread(Transaction tx,ArrayList result,String ds, String url, String sql){
106             this.tx = tx;
107             this.result = result;
108             this.dsName = ds;
109             this.url = url;
110             this.sql = sql;
111         }
112         
113         public void run(){
114             Connection conn = null;
115             try{
116                 TransactionManagerImpl tm = (TransactionManagerImpl)TransactionManagerImpl.getTransactionManager();
117                 tm.internalResume((TransactionImpl)tx);
118                 DriverTest test = new DriverTest();
119                 conn = test.getConnection(url, dsName);
120                 test.executeInsertInPSMT(conn, sql);
121                 conn.close();
122                 tm.internalSuspend();
123                 result.add("OK");
124             }catch(Exception e){
125                 result.add("NA");
126                 e.printStackTrace();
127             }finally{
128                 try{
129                     if(conn != null)
130                         conn.close();
131                 }catch(Exception e){
132                     e.printStackTrace();
133                 }
134             }
135         }
136     }
137 }
138 
139 

上一页  1 2 3 4  下一页

Tags:如何 Weblogic 全局

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接