Skip to content

[Bug]: "Commit not allowed here" error when using transactions #44219

@anuruddhal

Description

@anuruddhal

Description

The following code block throws a compilation error: commit not allowed here

public isolated function processHeartbeat(types:Heartbeat heartbeat) returns types:HeartbeatResponse|error {

    transaction {
        sql:ExecutionResult updateResult = check dbClient->execute(`
                UPDATE runtimes 
                SET status = ${heartbeat.status}, 
                    last_heartbeat = ${heartbeat.timestamp}
                WHERE runtime_id = ${heartbeat.runtimeId}
            `);


        if updateResult.affectedRowCount == 0 {
            rollback;
            return error(string `Runtime ${heartbeat.runtimeId} not found`);
        }
        
        _ = check dbClient->execute(`
                DELETE FROM runtime_services WHERE runtime_id = ${heartbeat.runtimeId}
            `);

        check commit;

        return {
            acknowledged: true
        };

    } on fail error e {
        return error(string `Failed to process heartbeat for runtime ${heartbeat.runtimeId}`, e);
    }
}

The same code works when the if block is commented or an else block is added.

if updateResult.affectedRowCount == 0 {
            rollback;
            return error(string `Runtime ${heartbeat.runtimeId} not found`);
}

Steps to Reproduce

No response

Affected Version(s)

2201.12.7

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions