略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: PDO::inTransaction

2024-05-08

PDO::inTransaction

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

PDO::inTransaction 检查是否在一个事务内

说明

PDO::inTransaction(): bool

检查驱动内的一个事务当前是否处于激活。此方法仅对支持事务的数据库驱动起作用。

参数

此函数没有参数。

返回值

如果当前事务处于激活,则返回 true ,否则返回 false

add a noteadd a note

User Contributed Notes 2 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
Anonymous
1 year 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);

}

官方地址:https://www.php.net/manual/en/pdo.intransaction.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3