Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Databases (PDO)

PHP PDO transactions

Reference for PDO transactions in PHP: beginTransaction(), commit(), rollBack() and inTransaction(), with a safe try/catch pattern, savepoints and common pitfalls.

A transaction groups several SQL statements into one all-or-nothing unit. Either every change is saved with commit(), or none of them is, after rollBack(). Use one whenever a single task needs more than one write, such as moving money between two accounts or creating an order together with its lines.

PDO offers four methods on the connection: beginTransaction(), commit(), rollBack() and inTransaction(). The standard pattern wraps the work in try/catch and rolls back on any exception.

PHP
$pdo->beginTransaction();
try {
    // several writes...
    $pdo->commit();
} catch (Throwable $e) {
    $pdo->rollBack();
    throw $e;
}

Try it

PHP A transfer that rolls back on failure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP State checks, mistakes and savepoints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
PDO::beginTransaction(): bool
PDO::commit(): bool
PDO::rollBack(): bool
PDO::inTransaction(): bool
Method Description
beginTransaction() Turns off auto-commit. All following statements belong to the transaction.
commit() Saves every change made since beginTransaction() and ends the transaction.
rollBack() Discards every change made since beginTransaction() and ends the transaction.
inTransaction() Tells whether a transaction is active on this connection.

Return value: true on success. inTransaction() returns a boolean state.

Errors / Exceptions: PDOException if a transaction is already active when you call beginTransaction(), if there is none when you call commit() or rollBack() (PHP 8.0 and later), or if the driver does not support transactions.

Usage notes

  • Always roll back in a catch. An exception inside the transaction leaves it open; the connection otherwise stays in transaction mode, and a later beginTransaction() fails.
  • Transactions do not nest. PDO has no nested transactions; use SQL SAVEPOINT (as in the second example) or a counter in your own wrapper, and check inTransaction() before starting.
  • DDL may commit implicitly. On MySQL, CREATE TABLE, ALTER TABLE and DROP TABLE end the current transaction. If you call commit() afterwards, PHP 8 may raise "There is no active transaction". SQLite and PostgreSQL can roll DDL back.
  • MySQL needs InnoDB. MyISAM tables ignore transactions.
  • Keep them short. An open transaction holds locks; do not wait for user input or slow network calls inside one.
  • Without a transaction, each statement commits on its own (auto-commit), so a failure halfway leaves partly changed data.
  • Concurrency. Transactions do not stop two requests reading the same balance. Use SELECT ... FOR UPDATE (MySQL, PostgreSQL), atomic updates like balance = balance - ?, or a CHECK constraint as shown.
  • Unfetched results. Finish or close open SELECT cursors before commit() on drivers that keep them open.