Swan Lake Beta3 is here.
import ballerina/io;

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

public function main() returns error? {
    Update newUpdate = {
        updateIndex: 132,
        stockMnt: 3500
    };
    transaction {
        check exec(newUpdate);
        check commit;
    }
}

// Transactional function can only be called from a transactional context
transactional function exec(Update u) returns error? {
    // Transactional named workers starts a transaction branch
    // in the current transaction.
    transactional worker A {
        bar();
    }

}

transactional function bar() {
    io:println("bar() invoked");
}

Transactional Named Workers

A named worker within a transactional function can be declared as transactional. This will start a new transaction branch for the named worker, as with a distributed transaction.

import ballerina/io;
type Update record {
    int updateIndex;
    int stockMnt;
};
public function main() returns error? {
    Update newUpdate = {
        updateIndex: 132,
        stockMnt: 3500
    };
    transaction {
        check exec(newUpdate);
        check commit;
    }
}
transactional function exec(Update u) returns error? {

Transactional function can only be called from a transactional context

    transactional worker A {
        bar();
    }

Transactional named workers starts a transaction branch in the current transaction.

}
transactional function bar() {
    io:println("bar() invoked");
}
bal run transactional_named_workers.bal
bar() invoked
import ballerina/io;

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

public function main() returns error? {
    Update newUpdate = {
        updateIndex: 132,
        stockMnt: 3500
    };
    transaction {
        check exec(newUpdate);
        check commit;
    }
}

// Transactional function can only be called from a transactional context
transactional function exec(Update u) returns error? {
    // Transactional named workers starts a transaction branch
    // in the current transaction.
    transactional worker A {
        bar();
    }

}

transactional function bar() {
    io:println("bar() invoked");
}

Transactional Named Workers

A named worker within a transactional function can be declared as transactional. This will start a new transaction branch for the named worker, as with a distributed transaction.

import ballerina/io;
type Update record {
    int updateIndex;
    int stockMnt;
};
public function main() returns error? {
    Update newUpdate = {
        updateIndex: 132,
        stockMnt: 3500
    };
    transaction {
        check exec(newUpdate);
        check commit;
    }
}
transactional function exec(Update u) returns error? {

Transactional function can only be called from a transactional context

    transactional worker A {
        bar();
    }

Transactional named workers starts a transaction branch in the current transaction.

}
transactional function bar() {
    io:println("bar() invoked");
}
bal run transactional_named_workers.bal
bar() invoked
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