Swan Lake Beta3 is here.
import ballerina/io;

public function main() returns error? {
    // Short for `retry<DefaultRetryManager>(3)`.
    // If any of the `doStage1` and `doStage2` returns  `error:Retriable`,
    // the program will retry execution until execution succeeds without an `error:Retriable` error.
    // By default, it will retry 3 times with the `DefaultRetryManager`.
    retry transaction {
        check doStage1();
        check doStage2();
        check commit;
    }

}

function doStage1() returns error? {
    io:println("Stage1 completed");
}

function doStage2() returns error? {
    // Returns `error:Retriable` error for retrying.
    // To support custom errors, a custom implementation of the `RetryManager` is required.
    return error 'error:Retriable("Stage2 failed");

}

Retry Transaction Statement

Transactional errors are often transient: retrying will fix them. This works by creating a RetryManager object r, before executing the transaction. If the block fails with error e, it calls r.shouldRetry(e). If that returns true, then it executes the block again. retry has an optional type parameter giving class of RetryManager to create, and optional arguments to new DefaultRetryManager tries n times. retry can be used without transaction.

import ballerina/io;
public function main() returns error? {
    retry transaction {
        check doStage1();
        check doStage2();
        check commit;
    }

Short for retry<DefaultRetryManager>(3). If any of the doStage1 and doStage2 returns error:Retriable, the program will retry execution until execution succeeds without an error:Retriable error. By default, it will retry 3 times with the DefaultRetryManager.

}
function doStage1() returns error? {
    io:println("Stage1 completed");
}
function doStage2() returns error? {
    return error 'error:Retriable("Stage2 failed");

Returns error:Retriable error for retrying. To support custom errors, a custom implementation of the RetryManager is required.

}
bal run retry_transaction_statement.bal
Stage1 completed
Stage1 completed
Stage1 completed
Stage1 completed
error: Stage2 failed {}
import ballerina/io;

public function main() returns error? {
    // Short for `retry<DefaultRetryManager>(3)`.
    // If any of the `doStage1` and `doStage2` returns  `error:Retriable`,
    // the program will retry execution until execution succeeds without an `error:Retriable` error.
    // By default, it will retry 3 times with the `DefaultRetryManager`.
    retry transaction {
        check doStage1();
        check doStage2();
        check commit;
    }

}

function doStage1() returns error? {
    io:println("Stage1 completed");
}

function doStage2() returns error? {
    // Returns `error:Retriable` error for retrying.
    // To support custom errors, a custom implementation of the `RetryManager` is required.
    return error 'error:Retriable("Stage2 failed");

}

Retry Transaction Statement

Transactional errors are often transient: retrying will fix them. This works by creating a RetryManager object r, before executing the transaction. If the block fails with error e, it calls r.shouldRetry(e). If that returns true, then it executes the block again. retry has an optional type parameter giving class of RetryManager to create, and optional arguments to new DefaultRetryManager tries n times. retry can be used without transaction.

import ballerina/io;
public function main() returns error? {
    retry transaction {
        check doStage1();
        check doStage2();
        check commit;
    }

Short for retry<DefaultRetryManager>(3). If any of the doStage1 and doStage2 returns error:Retriable, the program will retry execution until execution succeeds without an error:Retriable error. By default, it will retry 3 times with the DefaultRetryManager.

}
function doStage1() returns error? {
    io:println("Stage1 completed");
}
function doStage2() returns error? {
    return error 'error:Retriable("Stage2 failed");

Returns error:Retriable error for retrying. To support custom errors, a custom implementation of the RetryManager is required.

}
bal run retry_transaction_statement.bal
Stage1 completed
Stage1 completed
Stage1 completed
Stage1 completed
error: Stage2 failed {}
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand