For the complete documentation index, see llms.txt. This page is also available as Markdown.

Transactions with MariaDB Connector/C

MariaDB Connector/C supports multi-statement transactions with manual commit and rollback by disabling auto-commit with mysql_autocommit() on the connection.

Developers can use MariaDB Connector/C to perform basic DML (Data Manipulation Language) operations inside a transaction with MariaDB database products.

Auto-Commit Behavior

By default, MariaDB Connector/C enables auto-commit. When auto-commit is enabled, each SQL statement is executed in its own transaction.

Multi-Statement Transactions

MariaDB Connector/C supports multi-statement transactions when auto-commit is disabled.

Disable auto-commit by calling mysql_autocommit() with a mode of 0:

mysql_autocommit(conn, 0);

When auto-commit is disabled, a new transaction is automatically started when the current transaction is manually committed or rolled back. The application does not need to manually start each new transaction with START TRANSACTION or BEGIN.

The transaction can be manually managed by performing the following operations:

Code Example: DML in a Transaction

UPDATE, INSERT, and DELETE are DML operations that modify data in a table.

The following code demonstrates how to execute several UPDATE statements on the example table within a single transaction with auto-commit disabled. All of the updates are committed together; if any update fails, the transaction is rolled back so none of them are applied.

The query below confirms the UPDATE of the example table:

spinner

Last updated

Was this helpful?