PHP 8.0.24 Released!

PDO::inTransaction

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

PDO::inTransaction Checks if inside a transaction

Description

public PDO::inTransaction(): bool

Checks if a transaction is currently active within the driver. This method only works 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 3 notes

up
2
jlh
2 years ago
Important note: This will only detect whether a transaction has been started using beginTransaction(). It will not be able to detect transactions started by any other means, for example by executing "START TRANSACTION".
up
0
dennis
3 days ago
At least for MySQL/MariaDB, inTransaction shows the real state of the transaction since 8.0
up
0
Anonymous
2 years ago
In addition to what jlh says,
even with SQLite3 which automatically starts transaction,
inTransaction() only works after beginTransaction().

<?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 beginTransaction()
   
$pdo->beginTransaction();
   
var_dump($pdo->inTransaction());echo "<br>";    // bool(true)  : after beginTransaction()
   
$pdo->rollBack();
   
var_dump($pdo->inTransaction());echo "<br>";    // bool(false) : after commit() or rollBack()

}catch (PDOException $e){

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

}catch (
Exception | ErrorException $e){

   
var_dump($e);

}
To Top