在 java 中调用多个预言机存储过程并保持数据一致性



此方法有一些我无法理解的问题,例如,如果层次结构中的任何过程调用引发异常,它不会回滚在以前的过程调用中所做的更改......请帮帮我

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;
}   

如果层次结构中的任何过程调用引发异常,它不会回滚在上一个过程调用中所做的更改

Ofc,您不会将回滚调用放在捕获 SQLException 块中。仅当其中一个请求不起作用而不引发异常时,才调用回滚方法。

此外,您在执行请求后永远不会提交更改,因此当您来到调用 connection.rollback(); 的 else 语句时,您实际上没有什么可回滚的,因为没有提交任何内容。

阅读此页面以获取有关如何处理提交/回滚的基本示例。

最新更新