3

In Ballerina we can identify whether a transaction was successful within "onCommit" and "onAbort" functions we provide. But this takes us away from the current method.

I want to be able to verify whether the transaction was successful or failed in the very next line after the transaction within the same method. In my scenario, I cannot have global variables to share states as well. I can think of workarounds such as using a boolean within the function, but outside the transaction.

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
}

Is there a better and cleaner way for me to know whether the transaction was successful right after the transaction as above?

Thanks in advance.

Erandi Ganepola
  • 303
  • 3
  • 12

2 Answers2

4

The only way to get the status of the Ballerina transaction is via the registered callback function. Why do you need to have it just after the transaction? You can achieve the same functionality within the registered handler function. Having a boolean variable is not correct as it does not captures the failures in prepare or commit/abort phases.

If you want to save some transaction related information, you can use the current transaction id and the callback functions will get called with that 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. 
}
Anupama Pathirage
  • 631
  • 2
  • 10
  • 22
  • Are oncommit functions run in the same thread as the initial method? Is it ok to run other transactions inside this oncommit methods? Because in my scenario next transaction depends on previous one. – Erandi Ganepola May 08 '18 at 06:43
  • Do not use transactions within these handler methods. In future we will add compile time checks to avoid transactions within the handler functions as it leads to complicated transaction blocks. If the second transaction depends on previous one, shouldn't those operations in both belong to one transaction? – Anupama Pathirage May 08 '18 at 09:09
1

Please check whether the following code will help you!

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

However, if you want to have the state of the transaction outside of the method, then I believe you will have to have a boolean variable outside of the above method.

M Reza
  • 18,350
  • 14
  • 66
  • 71
  • What I exactly want is to know whether the transaction has been committed. Because, AFAIK, a transaction is successful only after it is committed. But with this pattern, there can be an occasion where individual queries were successful (I'm deleting/upserting thousands of records in this scenario), but finally, an error occurred when committing (network error). In such a case, how can we be sure that the transaction was successful? or not? This method doesn't do that as I see. – Erandi Ganepola May 08 '18 at 05:37