Skip to content

[pdo_firebird] Added pdo_firebird_check_liveness #12757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,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
Comment on lines +1219 to +1229
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I downloaded the source from the following list and checked the included ibase.h:
https://firebirdsql.org/en/discontinued-versions/

It turns out that this feature was introduced since v3.0.


/* 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.