此方法有一些我无法理解的问题,例如,如果层次结构中的任何过程调用抛出异常,它不会回滚先前过程调用中所做的更改......请帮助我
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;
}
最佳答案
it doesn't rollback the changes made in previous procedure call if any of the procedure calls down the hierarchy throws an exception
Ofc,您没有将对回滚的调用放在 catch SQLException block 中。仅当您的某个请求不起作用且不引发异常时,您才调用回滚方法。
此外,您在执行请求后永远不会提交更改,因此当您到达调用 connection.rollback();
的 else 语句时,实际上没有任何内容可以回滚,因为没有提交任何内容。
阅读this page有一个如何处理提交/回滚的基本示例。
关于java - java调用多个oracle存储过程并保持数据一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12779819/