<p>Kevin Harwell <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11076">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Benjamin Keith Ford: Looks good to me, but someone else must approve
Joshua Colp: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_config_odbc: Avoid deadlock when max_connections = 1<br><br>Rather than calling ast_odbc_find_table() in the prepare callback, call<br>it beforehand and pass it in to the callback to avoid the need for a<br>second connection.<br><br>ASTERISK-28166 #close<br><br>Change-Id: I6f8a0b9990d636fd6bc1a92ed70f7050d2436202<br>---<br>M res/res_config_odbc.c<br>1 file changed, 21 insertions(+), 16 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c</span><br><span>index 62d8dc3..09eff21 100644</span><br><span>--- a/res/res_config_odbc.c</span><br><span>+++ b/res/res_config_odbc.c</span><br><span>@@ -574,6 +574,7 @@</span><br><span> const char *table;</span><br><span> const struct ast_variable *lookup_fields;</span><br><span> const struct ast_variable *update_fields;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct odbc_cache_tables *tableptr;</span><br><span> };</span><br><span> </span><br><span> static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)</span><br><span>@@ -583,29 +584,21 @@</span><br><span> const struct ast_variable *field;</span><br><span> struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);</span><br><span> SQLHSTMT stmt;</span><br><span style="color: hsl(0, 100%, 40%);">- struct odbc_cache_tables *tableptr;</span><br><span> </span><br><span> if (!sql) {</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- tableptr = ast_odbc_find_table(ups->database, ups->table);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!tableptr) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", ups->table, ups->database);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);</span><br><span> if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {</span><br><span> ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ast_odbc_release_table(tableptr);</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span> ast_str_set(&sql, 0, "UPDATE %s SET ", ups->table);</span><br><span> </span><br><span> for (field = ups->update_fields; field; field = field->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_odbc_find_column(tableptr, field->name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_odbc_find_column(ups->tableptr, field->name)) {</span><br><span> ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", field->name);</span><br><span> SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(field->name), 0, (void *)field->value, 0, NULL);</span><br><span> first = 0;</span><br><span>@@ -618,9 +611,8 @@</span><br><span> first = 1;</span><br><span> </span><br><span> for (field = ups->lookup_fields; field; field = field->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_odbc_find_column(tableptr, field->name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_odbc_find_column(ups->tableptr, field->name)) {</span><br><span> 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);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_odbc_release_table(tableptr);</span><br><span> SQLFreeHandle(SQL_HANDLE_STMT, stmt);</span><br><span> return NULL;</span><br><span> }</span><br><span>@@ -629,9 +621,6 @@</span><br><span> first = 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Done with the table metadata */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_odbc_release_table(tableptr);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> res = ast_odbc_prepare(obj, stmt, ast_str_buffer(sql));</span><br><span> if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {</span><br><span> if (res == SQL_ERROR) {</span><br><span>@@ -663,20 +652,36 @@</span><br><span> {</span><br><span> struct odbc_obj *obj;</span><br><span> SQLHSTMT stmt;</span><br><span style="color: hsl(0, 100%, 40%);">- struct update2_prepare_struct ups = { .database = database, .table = table, .lookup_fields = lookup_fields, .update_fields = update_fields, };</span><br><span style="color: hsl(120, 100%, 40%);">+ struct update2_prepare_struct ups = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .database = database,</span><br><span style="color: hsl(120, 100%, 40%);">+ .table = table,</span><br><span style="color: hsl(120, 100%, 40%);">+ .lookup_fields = lookup_fields,</span><br><span style="color: hsl(120, 100%, 40%);">+ .update_fields = update_fields,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span> struct ast_str *sql;</span><br><span> int res;</span><br><span> SQLLEN rowcount = 0;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ ups.tableptr = ast_odbc_find_table(database, table);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ups.tableptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", table, database);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (!(obj = ast_odbc_request_obj(database, 0))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_odbc_release_table(ups.tableptr);</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span> if (!(stmt = ast_odbc_prepare_and_execute(obj, update2_prepare, &ups))) {</span><br><span> ast_odbc_release_obj(obj);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_odbc_release_table(ups.tableptr);</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* We don't need the table anymore */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_odbc_release_table(ups.tableptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> res = SQLRowCount(stmt, &rowcount);</span><br><span> SQLFreeHandle(SQL_HANDLE_STMT, stmt);</span><br><span> ast_odbc_release_obj(obj);</span><br><span>@@ -690,7 +695,7 @@</span><br><span> }</span><br><span> </span><br><span> if (rowcount >= 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- return (int)rowcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ return (int) rowcount;</span><br><span> }</span><br><span> </span><br><span> return -1;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11076">change 11076</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/11076"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I6f8a0b9990d636fd6bc1a92ed70f7050d2436202 </div>
<div style="display:none"> Gerrit-Change-Number: 11076 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>