transactions - 在 Ballerina 中无需回调即可获取交易状态
问题描述
在 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
}
有没有更好更干净的方法让我在上述交易后立即知道交易是否成功?
提前致谢。
解决方案
获得 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.
}
推荐阅读
- javascript - 为什么我在 Bootstrap 中的轮播指示器不起作用?
- matlab - 在 MATLAB 中绘制傅里叶级数的直流分量
- java - 将此多播路由(在 Apache-camel 中)从核心 spring 转换为 spring boot 时出错
- javascript - 根据拖动动量 Javascript 找出函数应该被调用的频率
- html - 在引导程序 4 中调整 col 内的跨度值
- powershell - 有人可以帮助正确添加电子邮件功能吗
- sql-server - 识别行中不匹配的记录 - SQL Server
- solr - Hybris如何在SOLR中索引变体
- google-colaboratory - Google Colab 中的 Meshroom 但没有输出文件
- multithreading - 无法让 Asyncio 并行运行 2 个任务(在等待下一个查询完成时处理查询结果)