Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: 'on fail' block not triggered within a transaction #41192

Open
dsplayerX opened this issue Aug 11, 2023 · 2 comments
Open

[Bug]: 'on fail' block not triggered within a transaction #41192

dsplayerX opened this issue Aug 11, 2023 · 2 comments
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug

Comments

@dsplayerX
Copy link
Contributor

dsplayerX commented Aug 11, 2023

Description

I've encountered an issue while working with transactions in Ballerina involving two MySQL databases. The 'on fail' block is not being executed even when there is an error with commit/rollback operations of a transaction. Here is a simplified version of the code that demonstrates the problem:

// ... (create MySQL database clients)

public function main() returns error? {
    string str = "";
    transaction {
        str += "transaction started";

        // ... (execution of 2 SQL queries)

       // sleep to stop the sql server
       io:println(" > sleeping");
       runtime:sleep(10);
       // > Manually stop the sql server service
       io:println(" > resumed");

        check (trap commit);

        str += " -> transaction ended.";
    }
    on fail var e {
        io:println("failed - " + e.toString());
    }
    io:println(str);
}

Here I'm manually stopping the SQL server service during sleep to fail the "commit". I get the following error message but it is not caught by the 'on fail' block.

error: transaction rollback failed:Communications link failure during rollback(). Transaction resolution unknown.
        at ballerinai.transaction.0:notifyAbort(internal.bal:147)
           ballerinai.transaction.0:rollbackTransaction(internal.bal:141)
           dumindusameendra.app810_mysql_test.0:main(main.bal:31)

Steps to Reproduce

  1. Provide the code with the necessary MySQL configurations.
  2. execute 2 SQL queries that would be successful inside the transaction.
  3. Run the script.
  4. During runtime.sleep manually stop the SQL server.

Affected Version(s)

2201.7.0 (Swan Lake Update 7)

OS, DB, other environment details and versions

Windows, MySQL Server

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Aug 11, 2023
@pcnfernando pcnfernando added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. and removed Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime labels Aug 11, 2023
@pcnfernando
Copy link
Member

This is not an issue with the onfail alone, but seems to be interrelated to onfail and transaction.
The below works as expected

import ballerina/io;
function foo() {
  panic error("panic");
}


public function main() {
  do {
    check trap foo();
  } on fail {
    io:println("Caught error");
  }
}

@dsplayerX
Copy link
Contributor Author

The below also works as expected because the error is from foo().

import ballerina/io;
function foo() {
  panic error("panic");
}

public function main() {
  transaction {
    check foo();
    check commit;
  } on fail {
    io:println("Caught error");
  }
}

The issue is with catching commit/rollback operation failures. (Using the trap keyword doesn't work either.)

import ballerina/io;

public function main() {
  transaction {
    // do some operations
    check commit; // if commit fails the error will not be caught
  } on fail {
    io:println("Caught error");
  }
}

@pcnfernando pcnfernando removed their assignment Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Projects
None yet
Development

No branches or pull requests

3 participants