3

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

有没有更好更干净的方法让我在上述交易后立即知道交易是否成功?

提前致谢。

4

2 回答 2

4

获得 Ballerina 交易状态的唯一方法是通过注册的回调函数。为什么您需要在交易后立即拥有它?您可以在注册的处理程序函数中实现相同的功能。拥有一个布尔变量是不正确的,因为它没有捕获准备或提交/中止阶段的失败。

如果你想保存一些交易相关的信息,你可以使用当前的交易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. 
}
于 2018-05-08T05:59:14.963 回答
1

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

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

但是,如果您想在方法之外拥有事务的状态,那么我相信您必须在上述方法之外拥有一个布尔变量。

于 2018-05-08T03:33:27.520 回答