Swan Lake Beta3 is here.
import ballerina/io;

public function main() returns error? {
    transaction {
        check update();
        check commit;
    }
}

transactional function update() returns error? {
    check updateDatabase();
    //  Registers a commit handler to be invoked when the `commit` is executed.
    'transaction:onCommit(sendEmail);
    'transaction:onRollback(logError);

}

function updateDatabase() returns error? {
    io:println("Database updated");
}

isolated function sendEmail('transaction:Info info) {
    io:println("Email sent.");
}

isolated function logError('transaction:Info info,
                            error? cause, boolean willRetry) {
    io:println("Logged database update failure");
}

Commit/Rollback Handlers

Often code needs to get executed depending on whether a transaction committed. Testing the result of the commit within the transaction statement works, but inconvenient from a modularity perspective, particularly when you want to undo changes on rollback. This seems much worse in a distributed transaction, when transaction statement is in another program. Ballerina provides commit/rollback handlers which are functions that get run when decision whether to commit is known.

import ballerina/io;
public function main() returns error? {
    transaction {
        check update();
        check commit;
    }
}
transactional function update() returns error? {
    check updateDatabase();
    'transaction:onCommit(sendEmail);
    'transaction:onRollback(logError);

Registers a commit handler to be invoked when the commit is executed.

}
function updateDatabase() returns error? {
    io:println("Database updated");
}
isolated function sendEmail('transaction:Info info) {
    io:println("Email sent.");
}
isolated function logError('transaction:Info info,
                            error? cause, boolean willRetry) {
    io:println("Logged database update failure");
}
bal run commit_rollback_handlers.bal
Database updated
Email sent.
import ballerina/io;

public function main() returns error? {
    transaction {
        check update();
        check commit;
    }
}

transactional function update() returns error? {
    check updateDatabase();
    //  Registers a commit handler to be invoked when the `commit` is executed.
    'transaction:onCommit(sendEmail);
    'transaction:onRollback(logError);

}

function updateDatabase() returns error? {
    io:println("Database updated");
}

isolated function sendEmail('transaction:Info info) {
    io:println("Email sent.");
}

isolated function logError('transaction:Info info,
                            error? cause, boolean willRetry) {
    io:println("Logged database update failure");
}

Commit/Rollback Handlers

Often code needs to get executed depending on whether a transaction committed. Testing the result of the commit within the transaction statement works, but inconvenient from a modularity perspective, particularly when you want to undo changes on rollback. This seems much worse in a distributed transaction, when transaction statement is in another program. Ballerina provides commit/rollback handlers which are functions that get run when decision whether to commit is known.

import ballerina/io;
public function main() returns error? {
    transaction {
        check update();
        check commit;
    }
}
transactional function update() returns error? {
    check updateDatabase();
    'transaction:onCommit(sendEmail);
    'transaction:onRollback(logError);

Registers a commit handler to be invoked when the commit is executed.

}
function updateDatabase() returns error? {
    io:println("Database updated");
}
isolated function sendEmail('transaction:Info info) {
    io:println("Email sent.");
}
isolated function logError('transaction:Info info,
                            error? cause, boolean willRetry) {
    io:println("Logged database update failure");
}
bal run commit_rollback_handlers.bal
Database updated
Email sent.
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