Skip to content

Implement SQL_ATTR_CONNECTION_DEAD for ODBC #9353

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions ext/odbc/php_odbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,17 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
RETCODE ret;
UCHAR d_name[32];
SQLSMALLINT len;
SQLUINTEGER dead = SQL_CD_FALSE;

ret = SQLGetConnectAttr(db_conn->hdbc,
SQL_ATTR_CONNECTION_DEAD,
&dead, 0, NULL);
if (dead == SQL_CD_TRUE) {
/* Bail early here, since we know it's gone */
zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_len);
goto try_and_get_another_connection;
}
/* If the driver doesn't support it, fall back to old heuristic */
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, can't we assume that the driver supports SQL_ATTR_CONNECTION_DEAD if ret == SQL_SUCCESS? I mean, can't we skip the fallback (SQL_DATA_SOURCE_READ_ONLY) regardless of the value of dead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, the Db2i driver at least had a case where it would return SQL_CD_FALSE, but the heuristic would detect a failed connection. However, that case (specifically, admin killing the database server process) is pretty weird and uncommon (though people bug me enough about it I have to mention it). You're probably right; it'd be better to just trust SQL_ATTR_CONNECTION_DEAD, and get driver authors to implement more accurate results if they're going to use it.

ret = SQLGetInfo(db_conn->hdbc,
SQL_DATA_SOURCE_READ_ONLY,
d_name, sizeof(d_name), &len);
Expand Down
11 changes: 9 additions & 2 deletions ext/pdo_odbc/odbc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,18 @@ static zend_result odbc_handle_check_liveness(pdo_dbh_t *dbh)
RETCODE ret;
UCHAR d_name[32];
SQLSMALLINT len;
SQLUINTEGER dead = SQL_CD_FALSE;
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;

ret = SQLGetConnectAttr(H->dbc, SQL_ATTR_CONNECTION_DEAD, &dead, 0, NULL);
if (dead == SQL_CD_TRUE) {
/* Bail early here, since we know it's gone */
return FAILURE;
}
/*
* SQL_ATTR_CONNECTION_DEAD is tempting, but only in ODBC 3.5,
* and not all drivers implement it properly
* If the driver doesn't support SQL_ATTR_CONNECTION_DEAD, fall back
* to using SQL_DATA_SOURCE_READ_ONLY, which isn't semantically
* correct, but works with many drivers.
*/
ret = SQLGetInfo(H->dbc, SQL_DATA_SOURCE_READ_ONLY, d_name,
sizeof(d_name), &len);
Expand Down