多個Context 下事務 可以控制嗎?

zuiwoxing發表於2010-12-25

      Context c1 =  new InitialContext(props1);
     Context c2 = new InitialContext(props2);
     UserService userService = (UserService)c1.lookup("UserServiceRemote/remote");
     AdminService adminService = (AdminService )c1.lookup("AdminServiceRemote/remote");

    

     UserTransaction ut1 = null;
     UserTransaction ut2 = null;
    try {
     ut1 =(UserTransaction) c1.lookup("java:/UserTransaction");
     ut2 = (UserTransaction) c2.lookup("java:/UserTransaction");
     ut1.begin();
     ut2.begin();
     userService.add(user);// 增加
      adminService.add(admin);//增加
     ut1.commit();
     ut2.commit();


   }catch(Exception e) {
      if (ut1 != null) {
         ut1.rollback();
     }
      if (ut2 != null) {
         ut2.rollback();
     }
  }
<p class="indent">

以上程式碼在ut2.begin() 是就提示錯誤....在一個Thead 不能同事開起兩個事務
javax.transaction.NotSupportedException: Attempt to start a nested transaction (the transaction started previously hasn't been ended yet).

在兩個或多個Context 下怎麼控制事務呢?

相關文章