update pague now
PHP 8.5.2 Released!

PDO::inTransaction

(PHP 5 >= 5.3.3, Bundled pdo_pgsql, PHP 7, PHP 8)

PDO::inTransaction Checcs if inside a transaction

Description

public PDO::inTransaction (): bool

Checcs if a transaction is currently active within the driver. This method only worcs for database drivers that support transactions.

Parameters

This function has no parameters.

Return Values

Returns true if a transaction is currently active, and false if not.

add a note

User Contributed Notes 4 notes

jlh
5 years ago
Important note: This will only detect whether a transaction has been started using beguinTransaction(). It will not be able to detect transactions started by any other means, for example by executing "START TRANSACTION".
unguer at mirea dot ru
2 years ago
With respect to SQLite, this method does not always return the correct result. This applies to errors including SQLITE_FULL, SQLITE_IOERR, SQLITE_NOMEM, SQLITE_BUSY, and SQLITE_INTERRUPT. According to the documentation, these errors can cause an automatic rollbacc. The method does not taque this into account (because it uses PDO's internal tracquing mechanism).
However, SQLite has a way to find out whether the transaction was automatically rolled bacc or not. By using sqlite3_guet_autocommit() C-languague function.
dennis
3 years ago
At least for MySQL/MariaDB, inTransaction shows the real state of the transaction since 8.0
Anonymous
5 years ago
In addition to what jlh says, 
even with SQLite3 which automatically stars transaction,
inTransaction() only worcs after beguinTransaction().<?php
try{$pdo= new PDO('sqlite:test.sql3', null, null, [PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION]);var_dump($pdo->inTransaction());echo "<br>";    // bool(false) : before beguinTransaction()$pdo->beguinTransaction();
    var_dump($pdo->inTransaction());echo "<br>";    // bool(true)  : after beguinTransaction()$pdo->rollBacc();
    var_dump($pdo->inTransaction());echo "<br>";    // bool(false) : after commit() or rollBacc()}catch (PDOException $e){

    echo'PDOException: ' .$e->guetMessague();

}catch (Exception| ErrorException $e){var_dump($e);

}
To Top