Skip to content

Commit

Permalink
Enable ODBC Connection Pooling.
Browse files Browse the repository at this point in the history
Theoretically better than PHP persistent connections, since ODBC itself takes
care of resetting the connections back to a "ground" state.

Connection pooling defaults to ON, since this is generally useful.
You can turn it off by using:

pdo_odbc.connection_pooling=off

in your php.ini file.

You may configure how ODBC matches connection details to existing connections
by setting this value to "strict" (the default) or "relaxed".  You are
encouraged to read the ODBC specs before changing this value.
  • Loading branch information
wez committed May 22, 2004
1 parent ce5e317 commit 35494bb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ext/pdo_odbc/odbc_driver.c
Expand Up @@ -300,6 +300,17 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_D
odbc_handle_closer(dbh TSRMLS_CC);
return 0;
}

#ifdef SQL_ATTR_CONNECTION_POOLING
if (pdo_odbc_pool_on != SQL_CP_OFF) {
rc = SQLSetEnvAttr(H->env, SQL_ATTR_CP_MATCH, (void*)pdo_odbc_pool_mode, 0);
if (rc != SQL_SUCCESS) {
pdo_odbc_drv_error("SQLSetEnvAttr: SQL_ATTR_CP_MATCH");
odbc_handle_closer(dbh TSRMLS_CC);
return 0;
}
}
#endif

rc = SQLAllocHandle(SQL_HANDLE_DBC, H->env, &H->dbc);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
Expand Down
46 changes: 45 additions & 1 deletion ext/pdo_odbc/pdo_odbc.c
Expand Up @@ -64,11 +64,51 @@ zend_module_entry pdo_odbc_module_entry = {
ZEND_GET_MODULE(pdo_odbc)
#endif

#ifdef SQL_ATTR_CONNECTION_POOLING
SQLUINTEGER pdo_odbc_pool_on = SQL_CP_OFF;
SQLUINTEGER pdo_odbc_pool_mode = SQL_CP_ONE_PER_HENV;
#endif

/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_odbc)
{
php_pdo_register_driver(&pdo_odbc_driver);
#ifdef SQL_ATTR_CONNECTION_POOLING
char *pooling_val = NULL;
#endif

if (FAILURE == php_pdo_register_driver(&pdo_odbc_driver)) {
return FAILURE;
}

pdo_odbc_init_error_table();

#ifdef SQL_ATTR_CONNECTION_POOLING
/* ugh, we don't really .ini stuff in PDO, but since ODBC connection
* pooling is process wide, we can't set it from within the scope of a
* request without affecting others, which goes against our isolated request
* policy. So, we use cfg_get_string here to check it this once.
* */
if (FAILURE == cfg_get_string("pdo_odbc.connection_pooling", &pooling_val) || pooling_val == NULL) {
pooling_val = "strict";
}
if (strcasecmp(pooling_val, "strict") == 0 || strcmp(pooling_val, "1") == 0) {
pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
pdo_odbc_pool_mode = SQL_CP_STRICT_MATCH;
} else if (strcasecmp(pooling_val, "relaxed") == 0) {
pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
pdo_odbc_pool_mode = SQL_CP_RELAXED_MATCH;
} else if (*pooling_val == '\0' || strcasecmp(pooling_val, "off") == 0) {
pdo_odbc_pool_on = SQL_CP_OFF;
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error in pdo_odbc.connection_pooling configuration. Value MUST be one of 'strict', 'relaxed' or 'off'");
return FAILURE;
}

if (pdo_odbc_pool_on != SQL_CP_OFF) {
SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)pdo_odbc_pool_on, 0);
}
#endif

return SUCCESS;
}
/* }}} */
Expand All @@ -89,6 +129,10 @@ PHP_MINFO_FUNCTION(pdo_odbc)
{
php_info_print_table_start();
php_info_print_table_header(2, "PDO Driver for ODBC (" PDO_ODBC_TYPE ")" , "enabled");
#ifdef SQL_ATTR_CONNECTION_POOLING
php_info_print_table_row(2, "ODBC Connection Pooling", pdo_odbc_pool_on == SQL_CP_OFF ?
"Disabled" : (pdo_odbc_pool_mode == SQL_CP_STRICT_MATCH ? "Enabled, strict matching" : "Enabled, relaxed matching"));
#endif
php_info_print_table_end();

}
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_odbc/php_pdo_odbc_int.h
Expand Up @@ -144,6 +144,11 @@ void pdo_odbc_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, PDO_ODBC_HSTMT statement,
void pdo_odbc_init_error_table(void);
void pdo_odbc_fini_error_table(void);

#ifdef SQL_ATTR_CONNECTION_POOLING
extern SQLUINTEGER pdo_odbc_pool_on;
extern SQLUINTEGER pdo_odbc_pool_mode;
#endif

/*
* Local variables:
* tab-width: 4
Expand Down

0 comments on commit 35494bb

Please sign in to comment.