0

*我正在尝试一个一个地运行三个异步任务。彼此相依为命。如果任何一项任务发生异常,则所有三个任务都应回滚。to 是以前的位置。但只有得到异常的任务才会回滚,而不是其他任务我的代码在下面

               public static int executeOrder() {
    int mOrderId=-1;
    Connection conn = null;
    JtdsCallableStatement jtdsproc = null;
    ResultSet orderResult = null;

    try {
        conn = ConnectionProvider.getConnection();
        jtdsproc = (JtdsCallableStatement) conn
                .prepareCall("{call SaveOrder(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
        jtdsproc.setInt(1, SqlOrderQueries.getORDERID());
        jtdsproc.setInt(2, SqlOrderQueries.getPOSID());
        jtdsproc.setInt(3, SqlOrderQueries.getEMP_CASH_ID());
        jtdsproc.setInt(4, SqlOrderQueries.getCUST_ID());
        jtdsproc.setInt(5, SqlOrderQueries.getTRANSTYPE());
        jtdsproc.setString(6, ""+SqlOrderQueries.getMDEBIT());
        jtdsproc.setString(7, ""+SqlOrderQueries.getADEBIT());
        jtdsproc.setString(8, ""+SqlOrderQueries.getMCREDIT());
        jtdsproc.setString(9, ""+SqlOrderQueries.getACREDIT());
        jtdsproc.setString(10, ""+SqlOrderQueries.getBCREDIT());
        jtdsproc.setInt(11, SqlOrderQueries.getMEALPLANID());
        jtdsproc.setInt(12, SqlOrderQueries.getCASHRESULTID());
        jtdsproc.setInt(13, SqlOrderQueries.getLUNCHTYPE());
        jtdsproc.setInt(14, SqlOrderQueries.getSCHOOL_ID());
        jtdsproc.setString(15, SqlOrderQueries.getORDERDATE());
        jtdsproc.setInt(16, SqlOrderQueries.getCREDITAUTH());

        if(SqlOrderQueries.getCHECKNUM()== -1)
        {
            jtdsproc.setNull(17, Types.INTEGER);
        }else{
            jtdsproc.setInt(17, SqlOrderQueries.getCHECKNUM());
        }
        jtdsproc.setInt(18, SqlOrderQueries.getOVERRIDE());
        jtdsproc.setInt(19, SqlOrderQueries.getVOID());
        jtdsproc.setInt(20, SqlOrderQueries.getORDLOG_ID());
        jtdsproc.setString(21, SqlOrderQueries.getORDLOGNOTE());
        orderResult = jtdsproc.executeQuery();
        while (orderResult.next()) {

            mOrderId=orderResult.getInt(1);
            Log.w("Data For Sales Handler: OrderId",
                    "$--" + orderResult.getInt(1));
            Log.w("Data For Sales Handler OrderIdLog:",
                    "$--" + orderResult.getInt(2));
            Log.w("Data For Sales Handler Customerid:",
                    "$--" + orderResult.getInt(3));
            Log.w("Data For Sales Handler Abalance:",
                    "$--" + orderResult.getFloat(4));
            Log.w("Data For Sales Handler Mbalance:",
                    "$--" + orderResult.getFloat(5));
            Log.w("Data For Sales Handler Bonacebalance:", "$--"
                    + orderResult.getFloat(6));
            Log.w("Data For Sales Handler Error:",
                    "$--" + orderResult.getString(7));
        }
    } catch (Exception ee) {
        try {
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ee.printStackTrace();
        return mOrderId = Utilities.NETWORK_ERROR;
    }
    return mOrderId;
}
public static boolean insertItem(int mOrderid, ArrayList<Integer> itemsIds,
        ArrayList<Integer> menuItemIds, ArrayList<Integer> itemsQuantity,
        ArrayList<Integer> itemsSoldType, ArrayList<Float> itemsPrice) {
    boolean insertStatus = false;
    Connection conn = ConnectionProvider.getConnection();
    PreparedStatement pStmt = null;
    String insertString = "insert into items values (?,?,?,?,?,?,?,?,?,?)";
    try {
        conn.setAutoCommit(false);
        pStmt = conn.prepareStatement(insertString);
        for(int i=0;i<itemsIds.size();)
        {
            pStmt.setInt(1, itemsIds.get(i));
            pStmt.setInt(2, mOrderid);
            pStmt.setInt(3, menuItemIds.get(i));
            pStmt.setInt(4, itemsQuantity.get(i));
            pStmt.setString(5, ""+itemsPrice.get(i));
            pStmt.setString(6, ""+itemsPrice.get(i));
            pStmt.setString(7, ""+1.00);
            pStmt.setInt(8, 0);
            pStmt.setInt(9, itemsSoldType.get(i));
            pStmt.setNull(10, Types.NULL);
            int a = pStmt.executeUpdate();
            Log.d("no of row affected for insert", "" + a);

            i++;
        }
        insertStatus = true;
    } catch (Exception ee) {
        ee.printStackTrace();
        if (conn != null) {
            try {
                System.err.print("Transaction is being rolled back");
                conn.rollback();
            } catch (SQLException excep) {
                excep.printStackTrace();
            }
        }
        insertStatus=false;
    } finally {
        try {
            pStmt.close();
            conn.setAutoCommit(true);
        } catch (SQLException eee) {
            eee.printStackTrace();
        }
    }
    return insertStatus;
}
public static ArrayList<Integer> getNewItemId(int totalItems) {
    Connection conn = ConnectionProvider.getConnection();
    JtdsCallableStatement jtdsproc = null;
    ArrayList<Integer> itemsIds = new ArrayList<Integer>();
    for (int i = 0; i < totalItems; i++) {
        try {
            jtdsproc = (JtdsCallableStatement) conn
                    .prepareCall("{call FASTINDEXGEN(1,1,0)}");
            ResultSet rs = jtdsproc.executeQuery();
            while (rs.next()) {
                Log.d("Value", "" + rs.getInt(1));
                itemsIds.add(rs.getInt(1));
            }
        } catch (Exception ee) {
            ee.printStackTrace();
            if (conn != null) {
                try {
                    System.err.print("Transaction is being rolled back");
                    conn.rollback();
                } catch (SQLException excep) {
                    excep.printStackTrace();
                }
            }
            itemsIds=null;
        }
    }
    return itemsIds;
}*



            public void completeOrder(InputBean input){

    setNO_PaYMENT(false);

    SqlOrderQueries.setPOSID(((HomeActivity)mActivity).mSessionManager.getPosId());
    SqlOrderQueries.setEMP_CASH_ID(((HomeActivity)mActivity).mSessionManager.getCustomerId());
    /*Customer id will be different for student and guest cash sale*/
    SqlOrderQueries.setCUST_ID(input.getCustomer_id());
    SqlOrderQueries.setTRANSTYPE(getTransType());
    SqlOrderQueries.setMDEBIT(input.getmDebit());
    SqlOrderQueries.setADEBIT(input.getaDebit());
    SqlOrderQueries.setMCREDIT(input.getmCredit());
    SqlOrderQueries.setACREDIT(input.getaCredit());
    SqlOrderQueries.setBCREDIT(""+0.00);

    /*Do not need to change the meal plan id ,it is default by default*/
    SqlOrderQueries.setCASHRESULTID(((HomeActivity)mActivity).mSessionManager.getCashColumnId());

    SqlOrderQueries.setLUNCHTYPE(DetailsFragment.getLUNCH_TYPE());
    SqlOrderQueries.setSCHOOL_ID(((HomeActivity)mActivity).mSessionManager.getSchoolId());
    SqlOrderQueries.setORDERDATE(getDate());
    /*Credit auth is passed default for the time being*/
    SqlOrderQueries.setCHECKNUM(input.getCheckNumber());
    /*override is passed 0 for the time being*/
    /*void ordlog_id  ordlognote  are  0 ,default ,default respectfully*/


    new AsyncTask<Void, Void, Integer>() {

        @Override
        protected Integer doInBackground(Void... params) {
            if(ConnectionProvider.checkConnection()==false){

                return null;
            }
            return OrderSalesHandler.executeOrder();
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);

            if(result == Utilities.NETWORK_ERROR)
            {
                AlertDialog.Builder mEAlert = new AlertDialog.Builder(mActivity);
                mEAlert.setMessage("Network Problem");
                mEAlert.create().show();
            }
            if(result>0){

                mItemshandler.onSucessOrder(result);
            }

        }


    }.execute();
}

@Override
public void onSucessOrder(int orderId) {

    mOrderid=orderId;
    new AsyncTask<Void, Void, ArrayList<Integer>>(){

        @Override
        protected ArrayList<Integer> doInBackground(Void... params) {
            if(ConnectionProvider.checkConnection()==false){
                return null;
            }
            return OrderSalesHandler.getNewItemId(mTotalItems);
        }

        @Override
        protected void onPostExecute(ArrayList<Integer> result) {
            super.onPostExecute(result);

            if(result==null)
            {
                AlertDialog.Builder mEAlert = new AlertDialog.Builder(mActivity);
                mEAlert.setMessage("Network Problem and Order not Completed");
                mEAlert.create().show();
            }else{

                mItemshandler.onSucessItemId(result);
            }

        }


    }.execute();
}

@Override
public void onSucessItemId(ArrayList<Integer> itemIds) {



    itemsIds=itemIds;
    new AsyncTask<Void, Void, Boolean>(){

        @Override
        protected Boolean doInBackground(Void... params) {

            if(ConnectionProvider.checkConnection()==false)
            {
                return false;
            }

            return OrderSalesHandler.insertItem(mOrderid,itemsIds,menuItemIds,itemsQuantity,itemsSoldType,itemsPrice);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if(result==true){

                if(isNO_PaYMENT()==false){

                    ((HomeActivity)mActivity).mSalesFragment.mPaymentDialog.dismiss();
                }

                ((HomeActivity)mActivity).mSalesFragment.onClick(((HomeActivity)mActivity).mSalesFragment.mCancelButton);


            }

            if(result==false){

                AlertDialog.Builder mEAlert = new AlertDialog.Builder(mActivity);
                mEAlert.setMessage("Network Problem and Order Not Completed");
                mEAlert.create().show();
            }
        }



    }.execute();
}



public static boolean isNO_PaYMENT() {
    return NO_PaYMENT;
}

public static void setNO_PaYMENT(boolean nO_PaYMENT) {
    NO_PaYMENT = nO_PaYMENT;
}

}

接口 InsertItemshandler {

public void onSucessOrder(int result);
public void onSucessItemId(ArrayList<Integer> itemsIds);

}

4

0 回答 0