Skip to content

Commit

Permalink
[pdo_firebird] Added pdo_firebird_check_liveness handler (php#12757)
Browse files Browse the repository at this point in the history
  • Loading branch information
SakiTakamachi authored Dec 20, 2023
1 parent 927adfb commit 5dfb2d9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ PHP 8.4 UPGRADE NOTES
Along with these, five constants (PDO::FB_TRANSACTION_ISOLATION_LEVEL,
PDO::FB_READ_COMMITTED, PDO::FB_REPEATABLE_READ, PDO::FB_SERIALIZABLE,
PDO::FB_WRITABLE_TRANSACTION) have been added.
. When using persistent connections, there is now a liveness check in the
constructor.

- PDO_MYSQL:
. getAttribute, enabled to get the value of ATTR_FETCH_TABLE_NAMES.
Expand Down
18 changes: 17 additions & 1 deletion ext/pdo_firebird/firebird_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,18 @@ static int pdo_firebird_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
}
/* }}} */

#if FB_API_VER >= 30
/* called by PDO to check liveness */
static zend_result pdo_firebird_check_liveness(pdo_dbh_t *dbh) /* {{{ */
{
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;

/* fb_ping return 0 if the connection is alive */
return fb_ping(H->isc_status, &H->db) ? FAILURE : SUCCESS;
}
/* }}} */
#endif

/* called by PDO to retrieve driver-specific information about an error that has occurred */
static void pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
{
Expand Down Expand Up @@ -1254,7 +1266,11 @@ static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
NULL, /* last_id not supported */
pdo_firebird_fetch_error_func,
pdo_firebird_get_attribute,
NULL, /* check_liveness */
#if FB_API_VER >= 30
pdo_firebird_check_liveness,
#else
NULL,
#endif
NULL, /* get driver methods */
NULL, /* request shutdown */
pdo_firebird_in_manually_transaction,
Expand Down
47 changes: 47 additions & 0 deletions ext/pdo_firebird/tests/persistent_connect.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
PDO_Firebird: persistent connect test
--EXTENSIONS--
pdo_firebird
--SKIPIF--
<?php require('skipif.inc'); ?>
--XLEAK--
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
See https://github.com/FirebirdSQL/firebird/issues/7849
--FILE--
<?php

/**
* Omit the case where the connection is broken when it checks liveness and
* it has to reconnect, as it is very difficult to reproduce the situation.
*/

require("testdb.inc");
unset($dbh);

$connIds = [];

foreach (['First', 'Second'] as $times) {
$dbh = new PDO(
PDO_FIREBIRD_TEST_DSN,
PDO_FIREBIRD_TEST_USER,
PDO_FIREBIRD_TEST_PASS,
[
PDO::ATTR_PERSISTENT => true,
],
);
$stmt = $dbh->query('SELECT CURRENT_CONNECTION FROM RDB$DATABASE');
$connId = $stmt->fetchColumn();
$connIds[] = $connId;
echo "{$times} connection ID: {$connId}\n";

unset($dbh);
unset($stmt);
unset($connID);
}

echo $connIds[0] === $connIds[1] ? "Same ID.\n" : "Different ID\n";
?>
--EXPECTF--
First connection ID: %d
Second connection ID: %d
Same ID.

0 comments on commit 5dfb2d9

Please sign in to comment.