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 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
13 changes: 13 additions & 0 deletions ext/odbc/php_odbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,20 @@ 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 (ret == SQL_SUCCESS && 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, or returns
* false (could be a false positive), fall back
* to the old heuristic.
*/
ret = SQLGetInfo(db_conn->hdbc,
SQL_DATA_SOURCE_READ_ONLY,
d_name, sizeof(d_name), &len);
Expand Down
12 changes: 10 additions & 2 deletions ext/pdo_odbc/odbc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,19 @@ 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 (ret == SQL_SUCCESS && 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, or if
* it returns false (which could be a false positive), 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