CascadiaPHP 2024

pg_transaction_status

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

pg_transaction_statusRetorna o status atual da transação do servidor

Descrição

pg_transaction_status(PgSql\Connection $connection): int

Retorna o status atual da transação do servidor.

Cuidado

pg_transaction_status() fornecerá resultados incorretos ao usar um servidor PostgreSQL 7.3 que tenha o parâmetro autocommit desativado. O recurso de confirmação automática do lado do servidor foi descontinuado e não existe em versões de servidor posteriores.

Parâmetros

connection

Uma instância de PgSql\Connection.

Valor Retornado

O status pode ser PGSQL_TRANSACTION_IDLE (atualmente inativo), PGSQL_TRANSACTION_ACTIVE (um comando está em andamento), PGSQL_TRANSACTION_INTRANS (inativo, em um bloco de transação válido), ou PGSQL_TRANSACTION_INERROR (ocioso, em um bloco de transação com falha). PGSQL_TRANSACTION_UNKNOWN é relatado se a conexão estiver ruim. PGSQL_TRANSACTION_ACTIVE é relatado somente quando uma consulta foi enviada ao servidor e ainda não foi concluída.

Registro de Alterações

Versão Descrição
8.1.0 O parâmetro connection agora espera uma instância de PgSql\Connection; anteriormente, um resource era esperado.

Exemplos

Exemplo #1 Exemplo de pg_transaction_status()

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Não foi possível conectar");
$stat = pg_transaction_status($dbconn);
if (
$stat === PGSQL_TRANSACTION_UNKNOWN) {
echo
'A conexão está ruim';
} else if (
$stat === PGSQL_TRANSACTION_IDLE) {
echo
'A conexão está inativa no momento';
} else {
echo
'A conexão está em estado de transação';
}
?>

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