2012-03-19 30 views
0

2フェーズ・コミットを実装しようとするサンプルのjdbcコードを作成中です。 DB2とXAの両方に準拠した2つのデータソースがあります。 2つの独立したjdbc接続が作成されています。私はAtomikosをTMとして使用しています。これらの接続を使用して、データソースが更新されます。ただし、更新クエリは実行されず、-911エラーを返します。なぜ、どこでデッドロックが発生しているのか理解できません。また、両方の接続は独立しており、関連していないので、なぜデッドロック/タイムアウトが発生しますか?DB2デッドロックの問題SQLCODE = -911、SQLERRMC = 68(XAトランザクション実行中)

.... 
    try{ 

     //Get the datasource connection using jndi custom class 

     JndiConn jcon1 = new JndiConn("jdbc/myDatasource1"); 
     JndiConn jcon2 = new JndiConn("jdbc/myDatasource2"); 
     UserTransactionImpl utx = new UserTransactionImpl(); 
     try{ 
      //Begin transaction    
      utx.begin(); 

      //Get the connection from the DB 
      conn1 = jcon1.ds.getConnection(); 
      conn2 = jcon2.ds.getConnection(); 

      //Reading the data from the form 
      int frmAccntNum = Integer.parseInt(req.getParameter("frmAccnt")); 
      int toAccntNum = Integer.parseInt(req.getParameter("toAccnt")); 
      int amt = Integer.parseInt(req.getParameter("amt")); 

      //Create a statement from the Connection 

      try{ 
       String selectQuery = "select AccountNumber, Balance from Accounts where AccountNumber =? with ur"; 
       PreparedStatement stmt = conn1.prepareStatement(selectQuery); 
       stmt.setInt(1,frmAccntNum); 
       ResultSet rs = stmt.executeQuery(); 
       rs.next(); 
       Account frmAccnt = new Account(rs.getInt(1),rs.getInt(2)); 
       int tempBal = frmAccnt.getBalance(); 

       PreparedStatement stmt2 = conn2.prepareStatement(selectQuery); 
       stmt2.setInt(1, toAccntNum); 
       ResultSet rs1 = stmt.executeQuery(); 
       rs1.next(); 
       Account toAccnt = new Account(rs1.getInt(1),rs1.getInt(2)); 
       int tempBal2 = toAccnt.getBalance(); 

       } 
       Operations t1 = new Operations(); 
       if(t1.checkAmt(frmAccnt,amt)){ 
        t1.Withdraw(frmAccnt, amt); 
        t1.Deposit(toAccnt, amt); 
       } 


       String updateQuery = "update Accounts set Balance = ? where AccountNumber= ? with ur"; 
       stmt = conn1.prepareStatement(updateQuery); 
       stmt.setInt(1, frmAccnt.getBalance()); 
       stmt.setInt(2,frmAccnt.getAccountNumber()); 
       stmt.executeUpdate(); 

       stmt2 = conn2.prepareStatement(updateQuery); 
       stmt2.setInt(1, toAccnt.getBalance()); 
       stmt2.setInt(2,toAccnt.getAccountNumber()); 
       stmt2.executeUpdate(); 
       //int r1 = stmt.executeUpdate("update Accounts set Balance = "+frmAccnt.getBalance()+"where AccountNumber="+frmAccnt.getAccountNumber()); 

       stmt.close(); 
       stmt2.close(); 

      }catch(SQLException sq){ 
       System.out.println("Setting Rollback true"); 
       sq.printStackTrace(); 
       rollback = true; 
      } 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     }finally{ 
      if(!rollback){ 
       try{ 
        utx.commit(); 
       }catch(Exception e){ 
        System.out.println("Commit Exception"); 
        e.printStackTrace(); 
        rollback = true; 
        try { 
         utx.rollback(); 
        } catch (Exception e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 
      } 
      else{ 
       try { 
        utx.rollback(); 
       } catch (Exception e2) { 
        // TODO Auto-generated catch block 
        e2.printStackTrace(); 
       } 
      } 
      try { 
       conn1.close(); 
       conn2.close(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    }catch(NamingException nme){ 
     nme.printStackTrace(); 
    } 
+0

があなたの前に開いRS1、および近いRS1前に(私は)それはrs.close(だと思う)を閉じるRSをしてみたいですupdateステートメントを発行します。それは役に立ちますか?何かのような音は閉じない。 – transistor1

+0

私はrsとrs1を閉じようとしましたが、結果は依然としてデッドロックです。 – Andy

答えて

1

それは、このためです:

ResultSet rs1 = stmt.executeQuery(); 

あなたは

ResultSet rs1 = stmt2.executeQuery(); 
+0

ああ...はい。それはそれを解決します。 Goshは何かそんなにばかげて見逃した:( Thnks !! – Andy

関連する問題