Skip to content
Snippets Groups Projects
Commit c38c8db1 authored by Sean Bright's avatar Sean Bright
Browse files

res_config_odbc: Avoid deadlock when max_connections = 1

Rather than calling ast_odbc_find_table() in the prepare callback, call
it beforehand and pass it in to the callback to avoid the need for a
second connection.

ASTERISK-28166 #close

Change-Id: I6f8a0b9990d636fd6bc1a92ed70f7050d2436202
parent c4a172ef
Branches
Tags
No related merge requests found
...@@ -576,6 +576,7 @@ struct update2_prepare_struct { ...@@ -576,6 +576,7 @@ struct update2_prepare_struct {
const char *table; const char *table;
const struct ast_variable *lookup_fields; const struct ast_variable *lookup_fields;
const struct ast_variable *update_fields; const struct ast_variable *update_fields;
struct odbc_cache_tables *tableptr;
}; };
static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data) static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
...@@ -585,29 +586,21 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data) ...@@ -585,29 +586,21 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
const struct ast_variable *field; const struct ast_variable *field;
struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE); struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
SQLHSTMT stmt; SQLHSTMT stmt;
struct odbc_cache_tables *tableptr;
if (!sql) { if (!sql) {
return NULL; return NULL;
} }
tableptr = ast_odbc_find_table(ups->database, ups->table);
if (!tableptr) {
ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", ups->table, ups->database);
return NULL;
}
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
ast_odbc_release_table(tableptr);
return NULL; return NULL;
} }
ast_str_set(&sql, 0, "UPDATE %s SET ", ups->table); ast_str_set(&sql, 0, "UPDATE %s SET ", ups->table);
for (field = ups->update_fields; field; field = field->next) { for (field = ups->update_fields; field; field = field->next) {
if (ast_odbc_find_column(tableptr, field->name)) { if (ast_odbc_find_column(ups->tableptr, field->name)) {
ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", field->name); ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", field->name);
SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(field->name), 0, (void *)field->value, 0, NULL); SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(field->name), 0, (void *)field->value, 0, NULL);
first = 0; first = 0;
...@@ -620,9 +613,8 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data) ...@@ -620,9 +613,8 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
first = 1; first = 1;
for (field = ups->lookup_fields; field; field = field->next) { for (field = ups->lookup_fields; field; field = field->next) {
if (!ast_odbc_find_column(tableptr, field->name)) { if (!ast_odbc_find_column(ups->tableptr, field->name)) {
ast_log(LOG_ERROR, "One or more of the criteria columns '%s' on '%s@%s' for this update does not exist!\n", field->name, ups->table, ups->database); ast_log(LOG_ERROR, "One or more of the criteria columns '%s' on '%s@%s' for this update does not exist!\n", field->name, ups->table, ups->database);
ast_odbc_release_table(tableptr);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
return NULL; return NULL;
} }
...@@ -631,9 +623,6 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data) ...@@ -631,9 +623,6 @@ static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
first = 0; first = 0;
} }
/* Done with the table metadata */
ast_odbc_release_table(tableptr);
res = ast_odbc_prepare(obj, stmt, ast_str_buffer(sql)); res = ast_odbc_prepare(obj, stmt, ast_str_buffer(sql));
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
if (res == SQL_ERROR) { if (res == SQL_ERROR) {
...@@ -665,20 +654,36 @@ static int update2_odbc(const char *database, const char *table, const struct as ...@@ -665,20 +654,36 @@ static int update2_odbc(const char *database, const char *table, const struct as
{ {
struct odbc_obj *obj; struct odbc_obj *obj;
SQLHSTMT stmt; SQLHSTMT stmt;
struct update2_prepare_struct ups = { .database = database, .table = table, .lookup_fields = lookup_fields, .update_fields = update_fields, }; struct update2_prepare_struct ups = {
.database = database,
.table = table,
.lookup_fields = lookup_fields,
.update_fields = update_fields,
};
struct ast_str *sql; struct ast_str *sql;
int res; int res;
SQLLEN rowcount = 0; SQLLEN rowcount = 0;
ups.tableptr = ast_odbc_find_table(database, table);
if (!ups.tableptr) {
ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", table, database);
return -1;
}
if (!(obj = ast_odbc_request_obj(database, 0))) { if (!(obj = ast_odbc_request_obj(database, 0))) {
ast_odbc_release_table(ups.tableptr);
return -1; return -1;
} }
if (!(stmt = ast_odbc_prepare_and_execute(obj, update2_prepare, &ups))) { if (!(stmt = ast_odbc_prepare_and_execute(obj, update2_prepare, &ups))) {
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
ast_odbc_release_table(ups.tableptr);
return -1; return -1;
} }
/* We don't need the table anymore */
ast_odbc_release_table(ups.tableptr);
res = SQLRowCount(stmt, &rowcount); res = SQLRowCount(stmt, &rowcount);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
...@@ -692,7 +697,7 @@ static int update2_odbc(const char *database, const char *table, const struct as ...@@ -692,7 +697,7 @@ static int update2_odbc(const char *database, const char *table, const struct as
} }
if (rowcount >= 0) { if (rowcount >= 0) {
return (int)rowcount; return (int) rowcount;
} }
return -1; return -1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment