Skip to content

Transactions

imshooter edited this page Nov 29, 2026 · 1 revision

A transaction gathers queries and runs of statements that apply all together or not at all. They run in order on one connection between START TRANSACTION and COMMIT. When one fails, the rest are not run, everything is rolled back, and OnQueryError is called with the statement that failed.

new
    MySQL:gDatabase,
    gPlayerId[MAX_PLAYERS]
;

forward OnItemBought(playerid);

stock BuyItem(playerid, item, price) {
    new const
        Transaction:transaction = mysql_transaction_create(gDatabase)
    ;

    mysql_transaction_add(transaction, "UPDATE players SET money = money - %i WHERE id = %i", price, gPlayerId[playerid]);
    mysql_transaction_add(transaction, "INSERT INTO inventory (player_id, item) VALUES (%i, %i)", gPlayerId[playerid], item);
    mysql_transaction_execute(transaction, "OnItemBought", "i", playerid);
}

public OnItemBought(playerid) {
    SendClientMessage(playerid, -1, "Bought.");

    return 1;
}

mysql_transaction_add_stmt adds a run of a statement of the same handle. mysql_transaction_query runs a transaction and waits for it. Each step gives a result of the cache.

Many writes that must stay in order run much faster in one transaction, because the server writes them to disk once.

Home

Guides

Reference

Functions

Connections
Queries
Cache
JSON cells
ORM
Queries built in pieces
Prepared statements
Batches
Transactions
Passwords
Callbacks

Clone this wiki locally