PHP 8.3.4 Released!

pg_transaction_status

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

pg_transaction_statusGibt den aktuellen Transaktionsstatus des Servers zurück

Beschreibung

pg_transaction_status(PgSql\Connection $connection): int

Gibt den aktuellen Transaktionsstatus des Servers zurück.

Achtung

Die Funktion pg_transaction_status() gibt ungültige Ergebnisse zurück, wenn sie für einen PostgreSQL-Server der Version 7.3 aufgerufen wird, bei dem der Parameter autocommit auf off gesetzt ist. Die serverseitige Eigenschaft autocommit ist veraltet und existiert in den neueren PostgreSQL-Versionen nicht mehr.

Parameter-Liste

connection

Eine PgSql\Connection-Instanz.

Rückgabewerte

Mögliche Stati sind: PGSQL_TRANSACTION_IDLE (zur Zeit untätig), PGSQL_TRANSACTION_ACTIVE (ein Kommando wird abgearbeitet), PGSQL_TRANSACTION_INTRANS (untätig, aber innerhalb eines gültigen Transaktionsblocks) oder PGSQL_TRANSACTION_INERROR (untätig, innerhalb einer fehlgeschlagenen Transaktion). PGSQL_TRANSACTION_UNKNOWN wird zurückgegeben, falls eine Transaktion ungültig ist. PGSQL_TRANSACTION_ACTIVE wird nur dann zurückgegeben, wenn eine Abfrage zum Server gesendet wurde und noch nicht fertig abgearbeitet ist.

Changelog

Version Beschreibung
8.1.0 Der Parameter connection erwartet nun eine PgSql\Connection-Instanz; vorher wurde eine Ressource erwartet.

Beispiele

Beispiel #1 pg_transaction_status()-Beispiel

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Konnte nicht verbinden");
$stat = pg_transaction_status($dbconn);
if (
$stat === PGSQL_TRANSACTION_UNKNOWN) {
echo
'Verbindung ist ungültig';
} else if (
$stat === PGSQL_TRANSACTION_IDLE) {
echo
'Verbindung ist zur Zeit untätig';
} else {
echo
'Verbindung meldet eine Transaktion';
}
?>

add a note

User Contributed Notes 2 notes

up
0
r dot grellmann at agentmulder dot de
2 years ago
After one of the asynchronous functions (pg_send_query(), pg_send_query_params()...) has been used, pg_transaction_status() will always report PGSQL_TRANSACTION_ACTIVE, no matter if a transaction was started or not.
Even after the first call (or all valid calls) to pg_get_result() the transaction status will stay PGSQL_TRANSACTION_ACTIVE, until either
- a synchronous function like pq_query() was used, or
- another call to pg_get_result() was made, which returns false.
Note: calling pg_free_result() does not change anything.

<?php
$conn
= pg_connect("dbname=publisher") or die("Could not connect");
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_IDLE
pg_send_query($conn, 'SELECT 1');
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_ACTIVE
$res = pg_get_result($conn);
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_ACTIVE
$res = pg_get_result($conn); // false
$status = pg_transaction_status($conn); // PGSQL_TRANSACTION_IDLE
?>
up
0
btherl at yahoo dot com dot au
17 years ago
This function is implemented in C, so there's no way to mimic it in SQL for older versions of PHP. But you can mimic some of the functionality by using a wrapper which keeps track of when you begin and commit/rollback transactions.
To Top