我正在研究一个尝试实现两阶段提交的示例jdbc代码。有2个数据源,兼容DB2和XA。创建了2个单独的jdbc连接。我使用Atomikos作为TM。使用这些连接,将对数据源进行更新。但是,更新查询未执行并返回-911错误。我无法弄清楚为什么以及在哪里发生死锁。此外,两个连接都是独立的,不相关,为什么会有死锁/超时?
....
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 :(得分:1)
正因为如此:
ResultSet rs1 = stmt.executeQuery();
你想要
ResultSet rs1 = stmt2.executeQuery();