<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15453] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15453">15453</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-11-12 13:54:50 -0600 (Thu, 12 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>expand per-thread db caching to odbc</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiamod_sofiah">freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_regc">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_odbcc">freeswitch/trunk/src/switch_odbc.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -3537,9 +3537,10 @@
</span><span class="cx">                 {
</span><span class="cx">                         const char *cond = switch_event_get_header(event, &quot;condition&quot;);
</span><span class="cx"> 
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;EVENT_TRAP: IP change detected\n&quot;);
</del><span class="cx"> 
</span><span class="cx">                         if (cond &amp;&amp; !strcmp(cond, &quot;network-address-change&quot;) &amp;&amp; mod_sofia_globals.auto_restart) {
</span><ins>+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;EVENT_TRAP: IP change detected\n&quot;);
+
</ins><span class="cx">                                 const char *old_ip4 = switch_event_get_header_nil(event, &quot;network-address-previous-v4&quot;);
</span><span class="cx">                                 const char *new_ip4 = switch_event_get_header_nil(event, &quot;network-address-change-v4&quot;);
</span><span class="cx">                                 const char *old_ip6 = switch_event_get_header_nil(event, &quot;network-address-previous-v6&quot;);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiamod_sofiah"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -36,7 +36,8 @@
</span><span class="cx"> 
</span><span class="cx"> /*Defines etc..*/
</span><span class="cx"> /*************************************************************************************************************************************************************/
</span><del>-#define MANUAL_BYE
</del><ins>+#define MANUAL_BYE 1
+#define SQL_CACHE_TIMEOUT 300
</ins><span class="cx"> #define DEFAULT_NONCE_TTL 60
</span><span class="cx"> #define IREG_SECONDS 30
</span><span class="cx"> #define GATEWAY_SECONDS 1
</span><span class="lines">@@ -489,7 +490,7 @@
</span><span class="cx">         char *odbc_dsn;
</span><span class="cx">         char *odbc_user;
</span><span class="cx">         char *odbc_pass;
</span><del>-        switch_odbc_handle_t *master_odbc;
</del><ins>+        //        switch_odbc_handle_t *master_odbc;
</ins><span class="cx">         switch_queue_t *sql_queue;
</span><span class="cx">         char *acl[SOFIA_MAX_ACL];
</span><span class="cx">         uint32_t acl_count;
</span><span class="lines">@@ -673,7 +674,10 @@
</span><span class="cx"> 
</span><span class="cx"> typedef struct {
</span><span class="cx">         switch_core_db_t *db;
</span><ins>+        switch_odbc_handle_t *odbc_dbh;
</ins><span class="cx">         time_t last_used;
</span><ins>+        switch_mutex_t *mutex;
+        switch_memory_pool_t *pool;
</ins><span class="cx"> } sofia_cache_db_handle_t;
</span><span class="cx"> 
</span><span class="cx"> #define sofia_test_pflag(obj, flag) ((obj)-&gt;pflags[flag] ? 1 : 0)
</span><span class="lines">@@ -797,7 +801,7 @@
</span><span class="cx"> switch_status_t sofia_on_hangup(switch_core_session_t *session);
</span><span class="cx"> char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup);
</span><span class="cx"> void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip);
</span><del>-void sofia_glue_sql_close(sofia_profile_t *profile);
</del><ins>+void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune);
</ins><span class="cx"> int sofia_glue_init_sql(sofia_profile_t *profile);
</span><span class="cx"> char *sofia_overcome_sip_uri_weakness(switch_core_session_t *session, const char *uri, const sofia_transport_t transport, switch_bool_t uri_only, const char *params);
</span><span class="cx"> switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -960,7 +960,9 @@
</span><span class="cx">                 
</span><span class="cx">                 if (++loops &gt;= 1000) {
</span><span class="cx">                         if (++ireg_loops &gt;= IREG_SECONDS) {
</span><del>-                                sofia_reg_check_expire(profile, switch_epoch_time_now(NULL), 0);
</del><ins>+                                time_t now = switch_epoch_time_now(NULL);
+                                sofia_reg_check_expire(profile, now, 0);
+                                sofia_glue_sql_close(profile, now);
</ins><span class="cx">                                 ireg_loops = 0;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="lines">@@ -1261,7 +1263,7 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        sofia_glue_sql_close(profile);
</del><ins>+        sofia_glue_sql_close(profile, 0);
</ins><span class="cx">         su_home_unref(profile-&gt;home);
</span><span class="cx">         su_root_destroy(profile-&gt;s_root);
</span><span class="cx">         pool = profile-&gt;pool;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -3610,6 +3610,7 @@
</span><span class="cx"> {
</span><span class="cx">         char *test_sql = NULL;
</span><span class="cx">         switch_core_db_t *db = NULL;
</span><ins>+        switch_odbc_handle_t *odbc_dbh = NULL;
</ins><span class="cx"> 
</span><span class="cx">         char reg_sql[] =
</span><span class="cx">                 &quot;CREATE TABLE sip_registrations (\n&quot;
</span><span class="lines">@@ -3769,14 +3770,17 @@
</span><span class="cx">                         NULL        
</span><span class="cx">                 };
</span><span class="cx"> 
</span><del>-                if (!(profile-&gt;master_odbc = switch_odbc_handle_new(profile-&gt;odbc_dsn, profile-&gt;odbc_user, profile-&gt;odbc_pass))) {
</del><ins>+
+                if (!(odbc_dbh = switch_odbc_handle_new(profile-&gt;odbc_dsn, profile-&gt;odbc_user, profile-&gt;odbc_pass))) {
</ins><span class="cx">                         return 0;
</span><span class="cx">                 }
</span><del>-                if (switch_odbc_handle_connect(profile-&gt;master_odbc) != SWITCH_ODBC_SUCCESS) {
</del><ins>+
+                if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_ODBC_SUCCESS) {
</ins><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Connecting ODBC DSN: %s\n&quot;, profile-&gt;odbc_dsn);
</span><ins>+                        switch_odbc_handle_destroy(&amp;odbc_dbh);
</ins><span class="cx">                         return 0;
</span><span class="cx">                 }
</span><del>-
</del><ins>+                
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Connected ODBC DSN: %s\n&quot;, profile-&gt;odbc_dsn);
</span><span class="cx">                 
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_registrations where (contact like '%%TCP%%' &quot;
</span><span class="lines">@@ -3785,9 +3789,9 @@
</span><span class="cx">                                                                   &quot;like '%%' and mwi_user  like '%%' and mwi_host like '%%'&quot;, 
</span><span class="cx">                                                                   mod_sofia_globals.hostname);
</span><span class="cx"> 
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_registrations&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, reg_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_registrations&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, reg_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx">                 free(test_sql);
</span><span class="cx"> 
</span><span class="lines">@@ -3795,55 +3799,55 @@
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_subscriptions where hostname='%q' and network_ip like '%%' and network_port like '%%'&quot;, 
</span><span class="cx">                                                                   mod_sofia_globals.hostname);
</span><span class="cx"> 
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_subscriptions&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, sub_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_subscriptions&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, sub_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 free(test_sql);
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_dialogs where hostname='%q' and contact like '%%'&quot;, mod_sofia_globals.hostname);
</span><span class="cx"> 
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_dialogs&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, dialog_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_dialogs&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, dialog_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_presence where hostname='%q'&quot;, mod_sofia_globals.hostname);
</span><span class="cx"> 
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_presence&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, pres_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_presence&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, pres_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 free(test_sql);
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_authentication where hostname='%q' or last_nc &gt;= 0&quot;, mod_sofia_globals.hostname);
</span><span class="cx"> 
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_authentication&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, auth_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_authentication&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, auth_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx">                 free(test_sql);
</span><span class="cx"> 
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_shared_appearance_subscriptions where contact_str='' or hostname='%q' and network_ip like '%%'&quot;,
</span><span class="cx">                                                                   mod_sofia_globals.hostname);
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_shared_appearance_subscriptions&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, shared_appearance_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_shared_appearance_subscriptions&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, shared_appearance_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx">                 free(test_sql);
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">                 test_sql = switch_mprintf(&quot;delete from sip_shared_appearance_dialogs where contact_str='' or hostname='%q' and network_ip like '%%'&quot;,
</span><span class="cx">                                                                   mod_sofia_globals.hostname);
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, &quot;DROP TABLE sip_shared_appearance_dialogs&quot;, NULL);
-                        switch_odbc_handle_exec(profile-&gt;master_odbc, shared_appearance_dialogs_sql, NULL);
</del><ins>+                if (switch_odbc_handle_exec(odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(odbc_dbh, &quot;DROP TABLE sip_shared_appearance_dialogs&quot;, NULL);
+                        switch_odbc_handle_exec(odbc_dbh, shared_appearance_dialogs_sql, NULL);
</ins><span class="cx">                 }
</span><span class="cx">                 free(test_sql);
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">                 for (x = 0; indexes[x]; x++) {
</span><del>-                        switch_odbc_handle_exec(profile-&gt;master_odbc, indexes[x], NULL);
</del><ins>+                        switch_odbc_handle_exec(odbc_dbh, indexes[x], NULL);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -3952,44 +3956,73 @@
</span><span class="cx">                 switch_core_db_exec(db, &quot;create index if not exists sa_hostname on sip_authentication (hostname)&quot;, NULL, NULL, NULL);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
-                return profile-&gt;master_odbc ? 1 : 0;
</del><ins>+        if (odbc_dbh) {
+                switch_odbc_handle_destroy(&amp;odbc_dbh);
+                return 1;
</ins><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         if (db) {
</span><span class="cx">                 switch_core_db_close(db);
</span><span class="cx">                 return 1;
</span><del>-        } else {
-                return 0;
</del><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        return 0;        
+
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void sofia_glue_sql_close(sofia_profile_t *profile)
</del><ins>+void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune)
</ins><span class="cx"> {
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;master_odbc) {
-                switch_odbc_handle_destroy(&amp;profile-&gt;master_odbc);
-        } else {
-                switch_hash_index_t *hi;
-                const void *var;
-                void *val;
-                sofia_cache_db_handle_t *dbh = NULL;
</del><ins>+        switch_hash_index_t *hi;
+        const void *var;
+        void *val;
+        sofia_cache_db_handle_t *dbh = NULL;
+        int locked = 0;
+        char *key;
</ins><span class="cx"> 
</span><del>-                switch_mutex_lock(profile-&gt;ireg_mutex);
-        top:
-        for (hi = switch_hash_first(NULL, profile-&gt;db_hash); hi; hi = switch_hash_next(hi)) {
-                        switch_hash_this(hi, &amp;var, NULL, &amp;val);
-            if ((dbh = (sofia_cache_db_handle_t *) val)) {
-                                switch_core_db_close(dbh-&gt;db);
-                                dbh-&gt;db = NULL;
-                                switch_core_hash_delete(profile-&gt;db_hash, (char *)var);
</del><ins>+        switch_mutex_lock(profile-&gt;ireg_mutex);
+ top:
+        locked = 0;
+        
+        for (hi = switch_hash_first(NULL, profile-&gt;db_hash); hi; hi = switch_hash_next(hi)) {
+                switch_hash_this(hi, &amp;var, NULL, &amp;val);
+                key = (char *) var;
</ins><span class="cx"> 
</span><ins>+                if ((dbh = (sofia_cache_db_handle_t *) val)) {
+                        time_t diff = 0;
+
+                        if (prune &gt; 0 &amp;&amp; prune &gt; dbh-&gt;last_used) {
+                                diff = (time_t) prune - dbh-&gt;last_used;
+                        }
+                        
+                        if (diff &lt; SQL_CACHE_TIMEOUT) {
+                                continue;
+                        }
+
+                        if (switch_mutex_trylock(dbh-&gt;mutex) == SWITCH_STATUS_SUCCESS) {
+                                if (dbh-&gt;db) {
+                                        switch_core_db_close(dbh-&gt;db);
+                                        dbh-&gt;db = NULL;
+                                } else if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
+                                        switch_odbc_handle_destroy(&amp;dbh-&gt;odbc_dbh);
+                                }
+
+                                switch_core_hash_delete(profile-&gt;db_hash, key);
+                                switch_mutex_unlock(dbh-&gt;mutex);
+                                switch_core_destroy_memory_pool(&amp;dbh-&gt;pool);
</ins><span class="cx">                                 goto top;
</span><ins>+        
+                        } else {
+                                if (!prune) locked++;
+                                continue;
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                switch_mutex_unlock(profile-&gt;ireg_mutex);
</del><ins>+        }
</ins><span class="cx"> 
</span><ins>+        if (locked) {
+                goto top;
</ins><span class="cx">         }
</span><ins>+
+        switch_mutex_unlock(profile-&gt;ireg_mutex);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic)
</span><span class="lines">@@ -4026,45 +4059,85 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_core_db_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
</del><ins>+
+void sofia_glue_release_db_handle(sofia_cache_db_handle_t **dbh)
</ins><span class="cx"> {
</span><ins>+        if (dbh &amp;&amp; *dbh) {
+                switch_mutex_unlock((*dbh)-&gt;mutex);
+                *dbh = NULL;
+        }
+}
+
+sofia_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
+{
</ins><span class="cx">         switch_thread_id_t self = switch_thread_self();
</span><span class="cx">         char thread_str[256] = &quot;&quot;;
</span><del>-        switch_core_db_t *db = NULL;
</del><span class="cx">         sofia_cache_db_handle_t *dbh = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         snprintf(thread_str, sizeof(thread_str) - 1, &quot;%lu&quot;, (unsigned long)(intptr_t)self);
</span><span class="cx"> 
</span><span class="cx">         switch_mutex_lock(profile-&gt;ireg_mutex);
</span><del>-        if ((dbh = switch_core_hash_find(profile-&gt;db_hash, thread_str))) {
-                db = dbh-&gt;db;
-        } else {
-                dbh = switch_core_alloc(profile-&gt;pool, sizeof(*dbh));
-                dbh-&gt;db = switch_core_db_open_file(profile-&gt;dbname);
-                dbh-&gt;last_used = switch_epoch_time_now(NULL);
</del><ins>+        if (!(dbh = switch_core_hash_find(profile-&gt;db_hash, thread_str))) {
+                switch_memory_pool_t *pool = NULL;
+                switch_core_db_t *db = NULL;
+                switch_odbc_handle_t *odbc_dbh = NULL;
+
+                if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
+                        if ((odbc_dbh = switch_odbc_handle_new(profile-&gt;odbc_dsn, profile-&gt;odbc_user, profile-&gt;odbc_pass))) {
+                                if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
+                                        switch_odbc_handle_destroy(&amp;odbc_dbh);
+                                }
+                        }
+                } else {
+                        db = switch_core_db_open_file(profile-&gt;dbname);
+                }
+
+                if (!db &amp;&amp; !odbc_dbh) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Failure!\n&quot;);
+                        goto end;
+                }
+
+                switch_core_new_memory_pool(&amp;pool);
+                dbh = switch_core_alloc(pool, sizeof(*dbh));
+                dbh-&gt;pool = pool;
+
+                
+                if (db) dbh-&gt;db = db; else dbh-&gt;odbc_dbh = odbc_dbh;
+                switch_mutex_init(&amp;dbh-&gt;mutex, SWITCH_MUTEX_NESTED, dbh-&gt;pool);
+                switch_mutex_lock(dbh-&gt;mutex);
+
</ins><span class="cx">                 switch_core_hash_insert(profile-&gt;db_hash, thread_str, dbh);
</span><del>-                db = dbh-&gt;db;
</del><span class="cx">         }
</span><ins>+
+ end:
+
+        if (dbh) dbh-&gt;last_used = switch_epoch_time_now(NULL);
+
</ins><span class="cx">         switch_mutex_unlock(profile-&gt;ireg_mutex);
</span><span class="cx"> 
</span><del>-        return db;
</del><ins>+        return dbh;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
</span><span class="cx"> {
</span><del>-        switch_core_db_t *db;
</del><ins>+        sofia_cache_db_handle_t *dbh = NULL;
</ins><span class="cx"> 
</span><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_lock(mutex);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
</del><ins>+        if (!(dbh = sofia_glue_get_db_handle(profile))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB\n&quot;);
+                goto end;
+        }
+
+        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
</ins><span class="cx">                 switch_odbc_statement_handle_t stmt;
</span><del>-                if (switch_odbc_handle_exec(profile-&gt;master_odbc, sql, &amp;stmt) != SWITCH_ODBC_SUCCESS) {
</del><ins>+                if (switch_odbc_handle_exec(dbh-&gt;odbc_dbh, sql, &amp;stmt) != SWITCH_ODBC_SUCCESS) {
</ins><span class="cx">                         char *err_str;
</span><del>-                        err_str = switch_odbc_handle_get_error(profile-&gt;master_odbc, stmt);
</del><ins>+                        err_str = switch_odbc_handle_get_error(dbh-&gt;odbc_dbh, stmt);
</ins><span class="cx">                         if (!zstr(err_str)) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ERR: [%s]\n[%s]\n&quot;, sql, err_str);
</span><span class="cx">                         }
</span><span class="lines">@@ -4076,21 +4149,19 @@
</span><span class="cx">         } else {
</span><span class="cx">                 char *errmsg;
</span><span class="cx"> 
</span><ins>+        switch_core_db_exec(dbh-&gt;db, sql, NULL, NULL, &amp;errmsg);
+
+        if (errmsg) {
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n%s\n&quot;, errmsg, sql);
+            switch_core_db_free(errmsg);
+        }
</ins><span class="cx">                 
</span><del>-                if (!(db = sofia_glue_get_db_handle(profile))) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, profile-&gt;dbname);
-                        goto end;
-                }
</del><ins>+        }
</ins><span class="cx"> 
</span><del>-                switch_core_db_exec(db, sql, NULL, NULL, &amp;errmsg);
</del><ins>+  end:
</ins><span class="cx"> 
</span><del>-                if (errmsg) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n%s\n&quot;, errmsg, sql);
-                        switch_core_db_free(errmsg);
-                }
-        }
</del><ins>+        sofia_glue_release_db_handle(&amp;dbh);
</ins><span class="cx"> 
</span><del>-  end:
</del><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_unlock(mutex);
</span><span class="cx">         }
</span><span class="lines">@@ -4100,28 +4171,25 @@
</span><span class="cx">                                                                                           switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
</span><span class="cx"> {
</span><span class="cx">         switch_bool_t ret = SWITCH_FALSE;
</span><del>-        switch_core_db_t *db;
</del><span class="cx">         char *errmsg = NULL;
</span><del>-
</del><ins>+        sofia_cache_db_handle_t *dbh = NULL;
+        
</ins><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_lock(mutex);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        if (!(dbh = sofia_glue_get_db_handle(profile))) {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB\n&quot;);
+        goto end;
+    }
</ins><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
-                switch_odbc_handle_callback_exec(profile-&gt;master_odbc, sql, callback, pdata);
</del><ins>+        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
+                switch_odbc_handle_callback_exec(dbh-&gt;odbc_dbh, sql, callback, pdata);
</ins><span class="cx">         } else if (profile-&gt;odbc_dsn) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ODBC IS NOT AVAILABLE!\n&quot;);
</span><span class="cx">         } else {
</span><ins>+                switch_core_db_exec(dbh-&gt;db, sql, callback, pdata, &amp;errmsg);
</ins><span class="cx"> 
</span><del>-                if (!(db = sofia_glue_get_db_handle(profile))) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, profile-&gt;dbname);
-                        goto end;
-                }
-
-
-                switch_core_db_exec(db, sql, callback, pdata, &amp;errmsg);
-
</del><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR: [%s] %s\n&quot;, sql, errmsg);
</span><span class="cx">                         free(errmsg);
</span><span class="lines">@@ -4129,46 +4197,43 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">   end:
</span><ins>+
+        sofia_glue_release_db_handle(&amp;dbh);
+
</ins><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_unlock(mutex);
</span><span class="cx">         }
</span><del>-        return ret;
-}
</del><span class="cx"> 
</span><del>-static char *sofia_glue_execute_sql2str_odbc(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
-{
-        char *ret = NULL;
-
-        if (switch_odbc_handle_exec_string(profile-&gt;master_odbc, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
-                ret = resbuf;
-        }
-
</del><span class="cx">         return ret;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
</span><span class="cx"> {
</span><del>-        switch_core_db_t *db;
</del><span class="cx">         switch_core_db_stmt_t *stmt;
</span><span class="cx">         char *ret = NULL;
</span><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
-                return sofia_glue_execute_sql2str_odbc(profile, mutex, sql, resbuf, len);
-        }
</del><ins>+        sofia_cache_db_handle_t *dbh = NULL;
</ins><span class="cx"> 
</span><ins>+        if (!(dbh = sofia_glue_get_db_handle(profile))) {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB\n&quot;);
+        goto end;
+    }
+
</ins><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_lock(mutex);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (!(db = switch_core_db_open_file(profile-&gt;dbname))) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, profile-&gt;dbname);
-                goto end;
</del><ins>+        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
+                if (switch_odbc_handle_exec_string(dbh-&gt;odbc_dbh, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
+                        return resbuf;
+                } else {
+                        goto end;
+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (switch_core_db_prepare(db, sql, -1, &amp;stmt, 0)) {
</del><ins>+        if (switch_core_db_prepare(dbh-&gt;db, sql, -1, &amp;stmt, 0)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Statement Error [%s]!\n&quot;, sql);
</span><del>-                goto fail;
</del><ins>+                goto end;
</ins><span class="cx">         } else {
</span><span class="cx">                 int running = 1;
</span><span class="cx">                 int colcount;
</span><span class="lines">@@ -4183,7 +4248,7 @@
</span><span class="cx">                                                 switch_copy_string(resbuf, (char *) txt, len);
</span><span class="cx">                                                 ret = resbuf;
</span><span class="cx">                                         } else {
</span><del>-                                                goto fail;
</del><ins>+                                                goto end;
</ins><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="cx">                                 break;
</span><span class="lines">@@ -4198,14 +4263,14 @@
</span><span class="cx">                 switch_core_db_finalize(stmt);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-  fail:
-        switch_core_db_close(db);
</del><ins>+ end:
</ins><span class="cx"> 
</span><del>-  end:
</del><span class="cx">         if (mutex) {
</span><span class="cx">                 switch_mutex_unlock(mutex);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        sofia_glue_release_db_handle(&amp;dbh);
+
</ins><span class="cx">         return ret;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_regc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -538,13 +538,6 @@
</span><span class="cx"> {
</span><span class="cx">         char sql[1024];
</span><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
-                if (!profile-&gt;master_odbc) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, profile-&gt;dbname);
-                        return;
-                }
-        }
-
</del><span class="cx">         switch_mutex_lock(profile-&gt;ireg_mutex);
</span><span class="cx"> 
</span><span class="cx">         if (now) {
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_odbcc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_odbc.c (15452 => 15453)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_odbc.c        2009-11-12 18:47:25 UTC (rev 15452)
+++ freeswitch/trunk/src/switch_odbc.c        2009-11-12 19:54:50 UTC (rev 15453)
</span><span class="lines">@@ -130,91 +130,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_connect(switch_odbc_handle_t *handle)
-{
-#ifdef SWITCH_HAVE_ODBC
-        int result;
-        SQLINTEGER err;
-        int16_t mlen;
-        unsigned char msg[200], stat[10];
-        SQLSMALLINT valueLength = 0;
-        int i = 0;
</del><span class="cx"> 
</span><del>-        if (handle-&gt;env == SQL_NULL_HANDLE) {
-                result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;handle-&gt;env);
-
-                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error AllocHandle\n&quot;);
-                        return SWITCH_ODBC_FAIL;
-                }
-
-                result = SQLSetEnvAttr(handle-&gt;env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
-
-                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error SetEnv\n&quot;);
-                        SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
-                        return SWITCH_ODBC_FAIL;
-                }
-
-                result = SQLAllocHandle(SQL_HANDLE_DBC, handle-&gt;env, &amp;handle-&gt;con);
-
-                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error AllocHDB %d\n&quot;, result);
-                        SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
-                        return SWITCH_ODBC_FAIL;
-                }
-                SQLSetConnectAttr(handle-&gt;con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
-        }
-        if (handle-&gt;state == SWITCH_ODBC_STATE_CONNECTED) {
-                switch_odbc_handle_disconnect(handle);
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Re-connecting %s\n&quot;, handle-&gt;dsn);
-        }
-
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Connecting %s\n&quot;, handle-&gt;dsn);
-
-        if (!strstr(handle-&gt;dsn, &quot;DRIVER&quot;)) {
-                result = SQLConnect(handle-&gt;con, (SQLCHAR *) handle-&gt;dsn, SQL_NTS, (SQLCHAR *) handle-&gt;username, SQL_NTS, (SQLCHAR *) handle-&gt;password, SQL_NTS);
-        } else {
-                SQLCHAR outstr[1024] = { 0 };
-                SQLSMALLINT outstrlen = 0;
-                result =
-                        SQLDriverConnect(handle-&gt;con, NULL, (SQLCHAR *) handle-&gt;dsn, (SQLSMALLINT) strlen(handle-&gt;dsn), outstr, sizeof(outstr), &amp;outstrlen,
-                                                         SQL_DRIVER_NOPROMPT);
-        }
-
-        if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
-                char *err_str;
-                if ((err_str = switch_odbc_handle_get_error(handle, NULL))) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;%s\n&quot;, err_str);
-                        free(err_str);
-                } else {
-                        SQLGetDiagRec(SQL_HANDLE_DBC, handle-&gt;con, 1, stat, &amp;err, msg, 100, &amp;mlen);
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error SQLConnect=%d errno=%d %s\n&quot;, result, (int) err, msg);
-                }
-                SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
-                return SWITCH_ODBC_FAIL;
-        }
-
-        result = SQLGetInfo(handle-&gt;con, SQL_DRIVER_NAME, (SQLCHAR *) handle-&gt;odbc_driver, 255, &amp;valueLength);
-        if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
-                for (i = 0; i &lt; valueLength; ++i)
-                        handle-&gt;odbc_driver[i] = (char) toupper(handle-&gt;odbc_driver[i]);
-        }
-
-        if (strstr(handle-&gt;odbc_driver, &quot;FIREBIRD&quot;) != 0 || strstr(handle-&gt;odbc_driver, &quot;FB32&quot;) != 0 || strstr(handle-&gt;odbc_driver, &quot;FB64&quot;) != 0) {
-                handle-&gt;is_firebird = TRUE;
-        } else {
-                handle-&gt;is_firebird = FALSE;
-        }
-
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Connected to [%s]\n&quot;, handle-&gt;dsn);
-        handle-&gt;state = SWITCH_ODBC_STATE_CONNECTED;
-        return SWITCH_ODBC_SUCCESS;
-#else
-        return SWITCH_ODBC_FAIL;
-#endif
-}
-
</del><span class="cx"> #ifdef SWITCH_HAVE_ODBC
</span><span class="cx"> static int db_is_up(switch_odbc_handle_t *handle)
</span><span class="cx"> {
</span><span class="lines">@@ -227,10 +143,11 @@
</span><span class="cx">         char *err_str = NULL;
</span><span class="cx">         SQLCHAR sql[255] = &quot;&quot;;
</span><span class="cx">         int max_tries = 120;
</span><del>-
</del><ins>+        int code = 0;
</ins><span class="cx">         SQLRETURN rc;
</span><span class="cx">         SQLSMALLINT nresultcols;
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx">   top:
</span><span class="cx"> 
</span><span class="cx">         if (!handle) {
</span><span class="lines">@@ -245,28 +162,33 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (SQLAllocHandle(SQL_HANDLE_STMT, handle-&gt;con, &amp;stmt) != SQL_SUCCESS) {
</span><ins>+                code = __LINE__;
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (SQLPrepare(stmt, sql, SQL_NTS) != SQL_SUCCESS) {
</span><ins>+                code = __LINE__;
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         result = SQLExecute(stmt);
</span><span class="cx"> 
</span><span class="cx">         if (result != SQL_SUCCESS &amp;&amp; result != SQL_SUCCESS_WITH_INFO) {
</span><ins>+                code = __LINE__;
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         SQLRowCount (stmt, &amp;m);
</span><span class="cx">         rc = SQLNumResultCols (stmt, &amp;nresultcols);
</span><span class="cx">         if (rc != SQL_SUCCESS){
</span><ins>+                code = __LINE__;
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx">         ret = (int) nresultcols;
</span><span class="cx">         /* determine statement type */
</span><span class="cx">         if (nresultcols &lt;= 0) {
</span><span class="cx">                 /* statement is not a select statement */
</span><ins>+                code = __LINE__;
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -279,10 +201,10 @@
</span><span class="cx">         max_tries--;
</span><span class="cx"> 
</span><span class="cx">         if (switch_event_create(&amp;event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
</span><del>-                switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Failure-Message&quot;, &quot;The sql server is not responding for DSN %s [%s]&quot;,
-                                                                switch_str_nil(handle-&gt;dsn), switch_str_nil(err_str));
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;The sql server is not responding for DSN %s [%s]\n&quot;,
-                                                  switch_str_nil(handle-&gt;dsn), switch_str_nil(err_str));
</del><ins>+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Failure-Message&quot;, &quot;The sql server is not responding for DSN %s [%s][%d]&quot;,
+                                                                switch_str_nil(handle-&gt;dsn), switch_str_nil(err_str), code);
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;The sql server is not responding for DSN %s [%s][%d]\n&quot;,
+                                                  switch_str_nil(handle-&gt;dsn), switch_str_nil(err_str), code);
</ins><span class="cx"> 
</span><span class="cx">                 if (recon == SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Additional-Info&quot;, &quot;The connection has been re-established&quot;);
</span><span class="lines">@@ -334,6 +256,91 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_connect(switch_odbc_handle_t *handle)
+{
+#ifdef SWITCH_HAVE_ODBC
+        int result;
+        SQLINTEGER err;
+        int16_t mlen;
+        unsigned char msg[200], stat[10];
+        SQLSMALLINT valueLength = 0;
+        int i = 0;
+
+        if (handle-&gt;env == SQL_NULL_HANDLE) {
+                result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;handle-&gt;env);
+
+                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error AllocHandle\n&quot;);
+                        return SWITCH_ODBC_FAIL;
+                }
+
+                result = SQLSetEnvAttr(handle-&gt;env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
+
+                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error SetEnv\n&quot;);
+                        SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
+                        return SWITCH_ODBC_FAIL;
+                }
+
+                result = SQLAllocHandle(SQL_HANDLE_DBC, handle-&gt;env, &amp;handle-&gt;con);
+
+                if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Error AllocHDB %d\n&quot;, result);
+                        SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
+                        return SWITCH_ODBC_FAIL;
+                }
+                SQLSetConnectAttr(handle-&gt;con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
+        }
+        if (handle-&gt;state == SWITCH_ODBC_STATE_CONNECTED) {
+                switch_odbc_handle_disconnect(handle);
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Re-connecting %s\n&quot;, handle-&gt;dsn);
+        }
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Connecting %s\n&quot;, handle-&gt;dsn);
+
+        if (!strstr(handle-&gt;dsn, &quot;DRIVER&quot;)) {
+                result = SQLConnect(handle-&gt;con, (SQLCHAR *) handle-&gt;dsn, SQL_NTS, (SQLCHAR *) handle-&gt;username, SQL_NTS, (SQLCHAR *) handle-&gt;password, SQL_NTS);
+        } else {
+                SQLCHAR outstr[1024] = { 0 };
+                SQLSMALLINT outstrlen = 0;
+                result =
+                        SQLDriverConnect(handle-&gt;con, NULL, (SQLCHAR *) handle-&gt;dsn, (SQLSMALLINT) strlen(handle-&gt;dsn), outstr, sizeof(outstr), &amp;outstrlen,
+                                                         SQL_DRIVER_NOPROMPT);
+        }
+
+        if ((result != SQL_SUCCESS) &amp;&amp; (result != SQL_SUCCESS_WITH_INFO)) {
+                char *err_str;
+                if ((err_str = switch_odbc_handle_get_error(handle, NULL))) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;%s\n&quot;, err_str);
+                        free(err_str);
+                } else {
+                        SQLGetDiagRec(SQL_HANDLE_DBC, handle-&gt;con, 1, stat, &amp;err, msg, 100, &amp;mlen);
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error SQLConnect=%d errno=%d %s\n&quot;, result, (int) err, msg);
+                }
+                SQLFreeHandle(SQL_HANDLE_ENV, handle-&gt;env);
+                return SWITCH_ODBC_FAIL;
+        }
+
+        result = SQLGetInfo(handle-&gt;con, SQL_DRIVER_NAME, (SQLCHAR *) handle-&gt;odbc_driver, 255, &amp;valueLength);
+        if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
+                for (i = 0; i &lt; valueLength; ++i)
+                        handle-&gt;odbc_driver[i] = (char) toupper(handle-&gt;odbc_driver[i]);
+        }
+
+        if (strstr(handle-&gt;odbc_driver, &quot;FIREBIRD&quot;) != 0 || strstr(handle-&gt;odbc_driver, &quot;FB32&quot;) != 0 || strstr(handle-&gt;odbc_driver, &quot;FB64&quot;) != 0) {
+                handle-&gt;is_firebird = TRUE;
+        } else {
+                handle-&gt;is_firebird = FALSE;
+        }
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Connected to [%s]\n&quot;, handle-&gt;dsn);
+        handle-&gt;state = SWITCH_ODBC_STATE_CONNECTED;
+        return SWITCH_ODBC_SUCCESS;
+#else
+        return SWITCH_ODBC_FAIL;
+#endif
+}
+
</ins><span class="cx"> SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_exec_string(switch_odbc_handle_t *handle,
</span><span class="cx">                                                                                                                                         char *sql,
</span><span class="cx">                                                                                                                                         char *resbuf,
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>