PDO::inTransaction

(PHP 5 >= 5.3.3, Bundled pdo_pgsql)

PDO::inTransaction Checks if inside a transaction

Description

public bool PDO::inTransaction ( void )

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.

Коментарии

Автор:
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".
2020-06-05 17:50:00
http://php5.kiev.ua/manual/ru/pdo.intransaction.html
Автор:
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'nullnull, [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);

}
2020-08-10 10:27:56
http://php5.kiev.ua/manual/ru/pdo.intransaction.html
Автор:
At least for MySQL/MariaDB, inTransaction shows the real state of the transaction since 8.0
2022-10-03 15:07:57
http://php5.kiev.ua/manual/ru/pdo.intransaction.html
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 rollback. The method does not take this into account (because it uses PDO's internal tracking mechanism).
However, SQLite has a way to find out whether the transaction was automatically rolled back or not. By using sqlite3_get_autocommit() C-language function.
2024-01-07 12:02:30
http://php5.kiev.ua/manual/ru/pdo.intransaction.html

    Поддержать сайт на родительском проекте КГБ