Skip to content

Commit 28a66b3

Browse files
NattyNarwhalnikic
authored andcommitted
Check liveness in PDO_ODBC
PDO drivers allow for it, and procedural ODBC has its own facility for it, but PDO_ODBC doesn't. If a connection is severed (for example, on IBM i, ending a databse job, or killing the network connection elsewhere), a persistent connection could get stuck. This adapts the procedural ODBC code to PDO for handling connection liveness, so PDO can reconnect if needed. A discussion about the method to check liveness is linked; this might not be the best method, but it's what procedural ODBC uses, so it's consistent. Closes GH-6805.
1 parent 93e6796 commit 28a66b3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

ext/pdo_odbc/odbc_driver.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,26 @@ static int odbc_handle_get_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
372372
return 0;
373373
}
374374

375+
static zend_result odbc_handle_check_liveness(pdo_dbh_t *dbh)
376+
{
377+
RETCODE ret;
378+
UCHAR d_name[32];
379+
SQLSMALLINT len;
380+
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
381+
382+
/*
383+
* SQL_ATTR_CONNECTION_DEAD is tempting, but only in ODBC 3.5,
384+
* and not all drivers implement it properly
385+
*/
386+
ret = SQLGetInfo(H->dbc, SQL_DATA_SOURCE_READ_ONLY, d_name,
387+
sizeof(d_name), &len);
388+
389+
if (ret != SQL_SUCCESS || len == 0) {
390+
return FAILURE;
391+
}
392+
return SUCCESS;
393+
}
394+
375395
static const struct pdo_dbh_methods odbc_methods = {
376396
odbc_handle_closer,
377397
odbc_handle_preparer,
@@ -384,7 +404,7 @@ static const struct pdo_dbh_methods odbc_methods = {
384404
NULL, /* last id */
385405
pdo_odbc_fetch_error_func,
386406
odbc_handle_get_attr, /* get attr */
387-
NULL, /* check_liveness */
407+
odbc_handle_check_liveness, /* check_liveness */
388408
NULL, /* get_driver_methods */
389409
NULL, /* request_shutdown */
390410
NULL, /* in transaction, use PDO's internal tracking mechanism */

0 commit comments

Comments
 (0)