1

This method has some issues I failed to understand, such as it doesn't rollback the changes made in previous procedure call if any of the procedure calls down the hierarchy throws an exception.... Please help me out

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

1 回答 1

1

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

Ofc,您不要将回滚调用放在 catch SQLException 块中。如果您的请求之一不工作而不会引发异常,您只会调用回滚方法。

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

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

于 2012-10-08T14:36:36.340 回答