首页 文章

发生错误后如何回滚事务

提问于
浏览
0

我有一个自定义项目,它创建了Journal Transaction屏幕的图形对象并输入数据 . 我想知道处理任何错误的最佳方法,如果发生这种情况,可以回滚所有条目 .

这是我如何插入行的代码片段(某些代码未显示):

Batch batch;
        GLTran tran;

        if (gltran.BatchNbr != lastbatchnbr)
        {
            batch = new Batch();
            batch.BranchID = branchID;
            batch.Description = "InterCo JE from " + osd.String01 + "Module AP Batch " + gltran.BatchNbr;
            batch.FinPeriodID = gltran.FinPeriodID;
            jegraph.BatchModule.Insert(batch);
            jegraph.Persist();
        }



        tran = new GLTran();
        tran.AccountID = accountID;
        tran.SubID = subID;
        tran.TranDate = gltran.TranDate;
        tran.RefNbr = gltran.RefNbr;
        tran.CuryDebitAmt = gltran.DebitAmt;
        tran.CuryCreditAmt = gltran.CreditAmt;

        jegraph.GLTranModuleBatNbr.Insert(tran);
        jegraph.Persist();

我应该坚持所有条目(插入)完成(对于几个批次) - 并且在任何持久性完成之前以某种方式回滚?

代码示例会有所帮助 . 谢谢 .

1 回答

  • 1

    如果您希望所有持久性一起出现,除非收到错误,否则应将持久代码包装在事务中 . 因此,您可以使用PXTransactionScope在同一事务中包装您拥有的代码(2个单独的持久性),如下所示 .

    using (PXTransactionScope ts = new PXTransactionScope())
    {
        // Persisting code here...
    
        ts.Complete();
    }
    

    收到错误后,它将自动回滚或不调用ts.Complete()

相关问题