Swan Lake Beta2 is here.
import ballerina/io;

type Update record {
    int updateIndex;
    int stockMnt;
};

public function main() returns error? {
    Update updates = {updateIndex: 0, stockMnt: 100};
    transaction {
        check doUpdate(updates);
        check commit;
    }
}

// Called within the transaction statement.
transactional function doUpdate(Update u) returns error? {
    // Calls the `foo()` non-transactional function.
    foo(u);
    // Calls the `bar()` transactional function.
    bar(u);

}

function foo(Update u) {
    if transactional {
        // This is a transactional context.
        bar(u);

    }
}

transactional function bar(Update u) {
    io:println("Calling from a transactional context");
}

Transactional Qualifier

At compile-time, regions of code are typed as being a transactional context. Ballerina guarantees that, whenever that region is executed, there will be a current transaction. A function with a transactional qualifier can only be called from transactional context; function body will be a transactional context. transactional is also a boolean expression that tests at runtime whether there is a current transaction: used in a condition results in transactional context.

import ballerina/io;
type Update record {
    int updateIndex;
    int stockMnt;
};
public function main() returns error? {
    Update updates = {updateIndex: 0, stockMnt: 100};
    transaction {
        check doUpdate(updates);
        check commit;
    }
}
transactional function doUpdate(Update u) returns error? {

Called within the transaction statement.

    foo(u);

Calls the foo() non-transactional function.

    bar(u);

Calls the bar() transactional function.

}
function foo(Update u) {
    if transactional {
        bar(u);

This is a transactional context.

    }
}
transactional function bar(Update u) {
    io:println("Calling from a transactional context");
}
bal run transactional_qualifier.bal
Calling from a transactional context
Calling from a transactional context
import ballerina/io;

type Update record {
    int updateIndex;
    int stockMnt;
};

public function main() returns error? {
    Update updates = {updateIndex: 0, stockMnt: 100};
    transaction {
        check doUpdate(updates);
        check commit;
    }
}

// Called within the transaction statement.
transactional function doUpdate(Update u) returns error? {
    // Calls the `foo()` non-transactional function.
    foo(u);
    // Calls the `bar()` transactional function.
    bar(u);

}

function foo(Update u) {
    if transactional {
        // This is a transactional context.
        bar(u);

    }
}

transactional function bar(Update u) {
    io:println("Calling from a transactional context");
}

Transactional Qualifier

At compile-time, regions of code are typed as being a transactional context. Ballerina guarantees that, whenever that region is executed, there will be a current transaction. A function with a transactional qualifier can only be called from transactional context; function body will be a transactional context. transactional is also a boolean expression that tests at runtime whether there is a current transaction: used in a condition results in transactional context.

import ballerina/io;
type Update record {
    int updateIndex;
    int stockMnt;
};
public function main() returns error? {
    Update updates = {updateIndex: 0, stockMnt: 100};
    transaction {
        check doUpdate(updates);
        check commit;
    }
}
transactional function doUpdate(Update u) returns error? {

Called within the transaction statement.

    foo(u);

Calls the foo() non-transactional function.

    bar(u);

Calls the bar() transactional function.

}
function foo(Update u) {
    if transactional {
        bar(u);

This is a transactional context.

    }
}
transactional function bar(Update u) {
    io:println("Calling from a transactional context");
}
bal run transactional_qualifier.bal
Calling from a transactional context
Calling from a transactional context

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