1
此方法有一些我不明白的問題,例如,如果任何過程調用層次結構時引發異常,它不會回滾以前過程調用中所做的更改....請幫我在java中調用多個oracle存儲過程並保持數據一致性
public synchronized boolean save(DTO dto) throws DAOException,IllegalArgumentException
{ boolean retVal=false;
boolean retVal1=false;
boolean retVal2=false;
boolean retVal5=true;
try{
connection=dataSource.getConnection();
connection.setAutoCommit(false);
cstmt=connection.prepareCall("{call PKG_ALL.PROC_MAIN(?,?,?)}");
cstmt.setString(1, "A"); cstmt.setString(2, "B");
cstmt.registerOutParameter(3,Types.VARCHAR);
ResultSet rs=cstmt.executeQuery();
String ErrMsg=cstmt.getString(3);
if(ErrMsg.equalsIgnoreCase("Record Inserted")) retVal=true;
else retVal=false;
cstmt.close();
cstmt1=connection.prepareCall("{call PKG_ALL.PROC_CHILD1(?,?,?)}");
cstmt1.setString(1, "A"); cstmt1.setString(2, "B");
cstmt1.registerOutParameter(3,Types.VARCHAR);
ResultSet rs1=cstmt.executeQuery();
String ErrMsg1=cstmt1.getString(3);
if(ErrMsg1.equalsIgnoreCase("Record Inserted")) retVal1=true;
else retVal1=false;
cstmt1.close();
if(strSerialNo!=null && strSerialNo.length > 0) // for a non-mandatory multirow in the form
{
cstmt2=connection.prepareCall("{call PKG_ALL.PROC_CHILD2(?,?,?)}");
for(int k=0;k<strSerialNo.length;k++)
{
cstmt2.setString(1,"M");
cstmt2.setString(2,"I");
cstmt2.registerOutParameter(3,Types.VARCHAR);
ResultSet rs2=cstmt2.executeQuery();
String ErrMsg2=cstmt2.getString(3);
if(ErrMsg2.equalsIgnoreCase("Record Inserted")) retVal2=true;
else
{
retVal5=false;
retVal2=false;
}
}
cstmt2.close();
}
**if(retVal&&retVal1&&retVal5)**
{
retVal=true;
connection.commit();
}
else
{
//connection.rollback();
retVal=false;
}
}
catch(SQLException e)
{
throw new DAOException(":"+e.getMessage());
}
catch(Exception e)
{
throw new DAOException(":"+e.getMessage());
}
finally
{
closeConnection(connection);
}
return retVal;
}
請告訴我缺少的代碼會是什麼樣子.. @ alain.janinm – user1728387
@ user1728387您可以使用connection.commit();在每個查詢之後。然後在catch塊中添加connection.rollback();如果(retVal && retVal1 && retVal5)可以擺脫您的狀況 –