Transactions 在Ballerina中获取事务的状态而不进行回调

Transactions 在Ballerina中获取事务的状态而不进行回调,transactions,ballerina,Transactions,Ballerina,在Ballerina中,我们可以在我们提供的“onCommit”和“onAbort”功能中确定事务是否成功。但这使我们脱离了目前的方法 我希望能够在同一方法中的事务之后的下一行中验证事务是成功的还是失败的。在我的场景中,我不能让全局变量也共享状态。我可以想到一些变通方法,比如在函数内部使用布尔值,但在事务外部使用布尔值 boolean status=true; transaction with retries = 4, oncommit = onCommitFunction, onabort =

在Ballerina中,我们可以在我们提供的“onCommit”和“onAbort”功能中确定事务是否成功。但这使我们脱离了目前的方法

我希望能够在同一方法中的事务之后的下一行中验证事务是成功的还是失败的。在我的场景中,我不能让全局变量也共享状态。我可以想到一些变通方法,比如在函数内部使用布尔值,但在事务外部使用布尔值

boolean status=true;
transaction with retries = 4, oncommit = onCommitFunction, onabort = onAbortFunction {
    status = true;
    // any sql statement/s here
    var result = client->update("INSERT ....");
   match result {
            int c => {
                io:println("Inserted count: " + c);
                if (c == 0) {
                    status = false;
                    abort;
                }
            }
            error err => {
                status = false;
                retry;
            }
    }
}
// Here I want to know whether the transaction was a success or a failure
if(status) {
    // success action
} else {
    // Failed action
}
是否有更好、更清晰的方法让我知道交易是否在上述交易之后成功


提前谢谢。

请检查以下代码是否对您有帮助

transaction with retries = 4, oncommit = onCommitFunction, onabort = onAbortFunction {
    // any sql statement/s here
    int result = client->insert("INSERT ....") but {error => -1};
    if (result < 0) {
        retry;
    } else if (resilt == 0) {
        abort;
    } else {
        // success action
    }
}
重试次数为4次的事务,oncommit=onCommitFunction,onabort=onAbortFunction{ //这里有sql语句吗 int result=client->insert(“insert….”)但{error=>-1}; 如果(结果<0){ 重试; }else if(重设==0){ 中止 }否则{ //成功行动 } }
但是,如果您想将事务的状态置于方法之外,那么我相信您必须在上述方法之外有一个布尔变量。

获取Ballerina事务状态的唯一方法是通过注册的回调函数。为什么在交易刚结束时你就需要它?您可以在注册的处理程序函数中实现相同的功能。使用布尔变量是不正确的,因为它不能捕获准备或提交/中止阶段的失败

如果要保存一些与事务相关的信息,可以使用当前事务id,并使用该id调用回调函数

transaction with retries = 4, oncommit = commitFunction, onabort = abortFunction {
    string txId = transactions:getCurrentTransactionId();
    //Store any information you need to access later using the txId - may be in a global map.
} onretry {
    //Get called before retrying
}

function commitFunction(string transactionid) {
    //Retrive the saved information using the transactionid. 
}

function abortFunction(string transactionid) {
    //Retrive the saved information using the transactionid. 
}

我真正想知道的是事务是否已提交。因为,一个事务只有在提交后才会成功。但是使用这种模式,可能会出现个别查询成功的情况(在这种情况下,我正在删除/升级数千条记录),但最后,在提交时发生了一个错误(网络错误)。在这种情况下,我们如何确保交易成功?还是不?正如我所看到的,这个方法不能做到这一点。oncommit函数是否与初始方法在同一线程中运行?是否可以在此oncommit方法内运行其他事务?因为在我的场景中,下一个事务依赖于上一个事务。不要在这些处理程序方法中使用事务。将来,我们将添加编译时检查,以避免处理程序函数中的事务,因为它会导致复杂的事务块。如果第二个事务依赖于前一个事务,那么这两个事务中的操作不应该属于一个事务吗?