ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.66k stars 748 forks source link

Transaction validates if rollback command is used with if condition, commit should happen in else block #26854

Closed niveathika closed 3 years ago

niveathika commented 3 years ago

Description: $subject This becomes an issue when we try to use statements within the loop, we have to wait till loop exit to commit, however, the transaction compiler does not allow that

niveathika commented 3 years ago
for var count in countArray{
    var queryResult = dbClient->query();
    if(queryResult is error) {
        rollback;
    }
}
commit;

The above is not allowed

pcnfernando commented 3 years ago

Need to restrict commiting/rollback within loops

pcnfernando commented 3 years ago

In order to commit/rollback, you have to have it outside the loop. We have restricted the usage from #27418. Furthermore, moving into a transactional code path from a non-transactional code path is also restricted. eg: Here commit is not allowed

transaction {
          fooTx();
          if something() {
             rollback;
             // not in transaction mode
             bazNonTx();
             // not in transaction mode
          }
          check commit;
        }