Transactions with MariaDB Connector/C++
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.
Confirm the auto-commit is enabled by calling sql::Connection::getAutoCommit():
bool isAutoCommit = conn->getAutoCommit();Multi-Statement Transactions
MariaDB Connector/C++ supports multi-statement transactions when the auto-commit is disabled.
Disable auto-commit by calling sql::Connection::setAutoCommit():
conn->setAutoCommit(false);When auto-commit is disabled, a new transaction is automatically started when the current transaction is manually committed or rolled back. It means 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:
Setting the transaction isolation by calling
sql::Connection::setTransactionIsolation()or using SET TRANSACTION ISOLATION LEVEL.Set the transaction to read-only by calling
sql::Connection::setReadOnly()or using SET TRANSACTION READ ONLY.Create a
savepointby callingsql::Connection::setSavepoint()or using SAVEPOINT.Roll back to a
savepointby callingsql::Connection::releaseSavepoint()or using RELEASE SAVEPOINT.Commit the transaction by calling
sql::Connection::commit()or using COMMIT.Roll back the transaction by calling
sql::Connection::rollback()or using ROLLBACK.
Code Example: DML in Transaction
UPDATE, INSERT, and DELETE are DML (Data Manipulation Language) operations that modify data in a table.
The following code demonstrates how to execute UPDATE on the example table within a transaction with auto-commit disabled.
To insert or delete data, replace the UPDATE statement in the code example with an INSERT or DELETE statement:
The query below confirms the UPDATE of the example table:
This page is: Copyright © 2025 MariaDB. All rights reserved.
Last updated
Was this helpful?

