<!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, "condition");
</span><span class="cx">
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
</del><span class="cx">
</span><span class="cx">                         if (cond && !strcmp(cond, "network-address-change") && mod_sofia_globals.auto_restart) {
</span><ins>+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
+
</ins><span class="cx">                                 const char *old_ip4 = switch_event_get_header_nil(event, "network-address-previous-v4");
</span><span class="cx">                                 const char *new_ip4 = switch_event_get_header_nil(event, "network-address-change-v4");
</span><span class="cx">                                 const char *old_ip6 = switch_event_get_header_nil(event, "network-address-previous-v6");
</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)->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 >= 1000) {
</span><span class="cx">                         if (++ireg_loops >= 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->home);
</span><span class="cx">         su_root_destroy(profile->s_root);
</span><span class="cx">         pool = profile->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">                 "CREATE TABLE sip_registrations (\n"
</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->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
</del><ins>+
+                if (!(odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
</ins><span class="cx">                         return 0;
</span><span class="cx">                 }
</span><del>-                if (switch_odbc_handle_connect(profile->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, "Error Connecting ODBC DSN: %s\n", profile->odbc_dsn);
</span><ins>+                        switch_odbc_handle_destroy(&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, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
</span><span class="cx">                 
</span><span class="cx">                 test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
</span><span class="lines">@@ -3785,9 +3789,9 @@
</span><span class="cx">                                                                  "like '%%' and mwi_user like '%%' and mwi_host like '%%'",
</span><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_registrations", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_registrations", 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("delete from sip_subscriptions where hostname='%q' and network_ip like '%%' and network_port like '%%'",
</span><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_subscriptions", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_subscriptions", 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("delete from sip_dialogs where hostname='%q' and contact like '%%'", mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_dialogs", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_dialogs", 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("delete from sip_presence where hostname='%q'", mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_presence", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_presence", 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("delete from sip_authentication where hostname='%q' or last_nc >= 0", mod_sofia_globals.hostname);
</span><span class="cx">
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_authentication", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_authentication", 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("delete from sip_shared_appearance_subscriptions where contact_str='' or hostname='%q' and network_ip like '%%'",
</span><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_shared_appearance_subscriptions", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_shared_appearance_subscriptions", 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("delete from sip_shared_appearance_dialogs where contact_str='' or hostname='%q' and network_ip like '%%'",
</span><span class="cx">                                                                  mod_sofia_globals.hostname);
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
-                        switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_shared_appearance_dialogs", NULL);
-                        switch_odbc_handle_exec(profile->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, "DROP TABLE sip_shared_appearance_dialogs", 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->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, "create index if not exists sa_hostname on sip_authentication (hostname)", NULL, NULL, NULL);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if (switch_odbc_available() && profile->odbc_dsn) {
-                return profile->master_odbc ? 1 : 0;
</del><ins>+        if (odbc_dbh) {
+                switch_odbc_handle_destroy(&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() && profile->master_odbc) {
-                switch_odbc_handle_destroy(&profile->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->ireg_mutex);
-        top:
- for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
-                        switch_hash_this(hi, &var, NULL, &val);
- if ((dbh = (sofia_cache_db_handle_t *) val)) {
-                                switch_core_db_close(dbh->db);
-                                dbh->db = NULL;
-                                switch_core_hash_delete(profile->db_hash, (char *)var);
</del><ins>+        switch_mutex_lock(profile->ireg_mutex);
+ top:
+        locked = 0;
+        
+        for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
+                switch_hash_this(hi, &var, NULL, &val);
+                key = (char *) var;
</ins><span class="cx">
</span><ins>+                if ((dbh = (sofia_cache_db_handle_t *) val)) {
+                        time_t diff = 0;
+
+                        if (prune > 0 && prune > dbh->last_used) {
+                                diff = (time_t) prune - dbh->last_used;
+                        }
+                        
+                        if (diff < SQL_CACHE_TIMEOUT) {
+                                continue;
+                        }
+
+                        if (switch_mutex_trylock(dbh->mutex) == SWITCH_STATUS_SUCCESS) {
+                                if (dbh->db) {
+                                        switch_core_db_close(dbh->db);
+                                        dbh->db = NULL;
+                                } else if (switch_odbc_available() && dbh->odbc_dbh) {
+                                        switch_odbc_handle_destroy(&dbh->odbc_dbh);
+                                }
+
+                                switch_core_hash_delete(profile->db_hash, key);
+                                switch_mutex_unlock(dbh->mutex);
+                                switch_core_destroy_memory_pool(&dbh->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->ireg_mutex);
</del><ins>+        }
</ins><span class="cx">
</span><ins>+        if (locked) {
+                goto top;
</ins><span class="cx">         }
</span><ins>+
+        switch_mutex_unlock(profile->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 && *dbh) {
+                switch_mutex_unlock((*dbh)->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] = "";
</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, "%lu", (unsigned long)(intptr_t)self);
</span><span class="cx">
</span><span class="cx">         switch_mutex_lock(profile->ireg_mutex);
</span><del>-        if ((dbh = switch_core_hash_find(profile->db_hash, thread_str))) {
-                db = dbh->db;
-        } else {
-                dbh = switch_core_alloc(profile->pool, sizeof(*dbh));
-                dbh->db = switch_core_db_open_file(profile->dbname);
-                dbh->last_used = switch_epoch_time_now(NULL);
</del><ins>+        if (!(dbh = switch_core_hash_find(profile->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() && profile->odbc_dsn) {
+                        if ((odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
+                                if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
+                                        switch_odbc_handle_destroy(&odbc_dbh);
+                                }
+                        }
+                } else {
+                        db = switch_core_db_open_file(profile->dbname);
+                }
+
+                if (!db && !odbc_dbh) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure!\n");
+                        goto end;
+                }
+
+                switch_core_new_memory_pool(&pool);
+                dbh = switch_core_alloc(pool, sizeof(*dbh));
+                dbh->pool = pool;
+
+                
+                if (db) dbh->db = db; else dbh->odbc_dbh = odbc_dbh;
+                switch_mutex_init(&dbh->mutex, SWITCH_MUTEX_NESTED, dbh->pool);
+                switch_mutex_lock(dbh->mutex);
+
</ins><span class="cx">                 switch_core_hash_insert(profile->db_hash, thread_str, dbh);
</span><del>-                db = dbh->db;
</del><span class="cx">         }
</span><ins>+
+ end:
+
+        if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
+
</ins><span class="cx">         switch_mutex_unlock(profile->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() && profile->odbc_dsn) {
</del><ins>+        if (!(dbh = sofia_glue_get_db_handle(profile))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+                goto end;
+        }
+
+        if (switch_odbc_available() && dbh->odbc_dbh) {
</ins><span class="cx">                 switch_odbc_statement_handle_t stmt;
</span><del>-                if (switch_odbc_handle_exec(profile->master_odbc, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
</del><ins>+                if (switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
</ins><span class="cx">                         char *err_str;
</span><del>-                        err_str = switch_odbc_handle_get_error(profile->master_odbc, stmt);
</del><ins>+                        err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
</ins><span class="cx">                         if (!zstr(err_str)) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", 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->db, sql, NULL, NULL, &errmsg);
+
+ if (errmsg) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", 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, "Error Opening DB %s\n", profile->dbname);
-                        goto end;
-                }
</del><ins>+        }
</ins><span class="cx">
</span><del>-                switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
</del><ins>+ end:
</ins><span class="cx">
</span><del>-                if (errmsg) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
-                        switch_core_db_free(errmsg);
-                }
-        }
</del><ins>+        sofia_glue_release_db_handle(&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, "Error Opening DB\n");
+ goto end;
+ }
</ins><span class="cx">
</span><del>-        if (switch_odbc_available() && profile->odbc_dsn) {
-                switch_odbc_handle_callback_exec(profile->master_odbc, sql, callback, pdata);
</del><ins>+        if (switch_odbc_available() && dbh->odbc_dbh) {
+                switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata);
</ins><span class="cx">         } else if (profile->odbc_dsn) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
</span><span class="cx">         } else {
</span><ins>+                switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
</ins><span class="cx">
</span><del>-                if (!(db = sofia_glue_get_db_handle(profile))) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                        goto end;
-                }
-
-
-                switch_core_db_exec(db, sql, callback, pdata, &errmsg);
-
</del><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", 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(&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->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() && profile->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, "Error Opening DB\n");
+ 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->dbname))) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                goto end;
</del><ins>+        if (switch_odbc_available() && dbh->odbc_dbh) {
+                if (switch_odbc_handle_exec_string(dbh->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, &stmt, 0)) {
</del><ins>+        if (switch_core_db_prepare(dbh->db, sql, -1, &stmt, 0)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", 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(&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() && profile->odbc_dsn) {
-                if (!profile->master_odbc) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                        return;
-                }
-        }
-
</del><span class="cx">         switch_mutex_lock(profile->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->env == SQL_NULL_HANDLE) {
-                result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &handle->env);
-
-                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHandle\n");
-                        return SWITCH_ODBC_FAIL;
-                }
-
-                result = SQLSetEnvAttr(handle->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
-
-                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error SetEnv\n");
-                        SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
-                        return SWITCH_ODBC_FAIL;
-                }
-
-                result = SQLAllocHandle(SQL_HANDLE_DBC, handle->env, &handle->con);
-
-                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHDB %d\n", result);
-                        SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
-                        return SWITCH_ODBC_FAIL;
-                }
-                SQLSetConnectAttr(handle->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
-        }
-        if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
-                switch_odbc_handle_disconnect(handle);
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Re-connecting %s\n", handle->dsn);
-        }
-
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting %s\n", handle->dsn);
-
-        if (!strstr(handle->dsn, "DRIVER")) {
-                result = SQLConnect(handle->con, (SQLCHAR *) handle->dsn, SQL_NTS, (SQLCHAR *) handle->username, SQL_NTS, (SQLCHAR *) handle->password, SQL_NTS);
-        } else {
-                SQLCHAR outstr[1024] = { 0 };
-                SQLSMALLINT outstrlen = 0;
-                result =
-                        SQLDriverConnect(handle->con, NULL, (SQLCHAR *) handle->dsn, (SQLSMALLINT) strlen(handle->dsn), outstr, sizeof(outstr), &outstrlen,
-                                                         SQL_DRIVER_NOPROMPT);
-        }
-
-        if ((result != SQL_SUCCESS) && (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, "%s\n", err_str);
-                        free(err_str);
-                } else {
-                        SQLGetDiagRec(SQL_HANDLE_DBC, handle->con, 1, stat, &err, msg, 100, &mlen);
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error SQLConnect=%d errno=%d %s\n", result, (int) err, msg);
-                }
-                SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
-                return SWITCH_ODBC_FAIL;
-        }
-
-        result = SQLGetInfo(handle->con, SQL_DRIVER_NAME, (SQLCHAR *) handle->odbc_driver, 255, &valueLength);
-        if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
-                for (i = 0; i < valueLength; ++i)
-                        handle->odbc_driver[i] = (char) toupper(handle->odbc_driver[i]);
-        }
-
-        if (strstr(handle->odbc_driver, "FIREBIRD") != 0 || strstr(handle->odbc_driver, "FB32") != 0 || strstr(handle->odbc_driver, "FB64") != 0) {
-                handle->is_firebird = TRUE;
-        } else {
-                handle->is_firebird = FALSE;
-        }
-
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connected to [%s]\n", handle->dsn);
-        handle->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] = "";
</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->con, &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 && 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, &m);
</span><span class="cx">         rc = SQLNumResultCols (stmt, &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 <= 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(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
</span><del>-                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Failure-Message", "The sql server is not responding for DSN %s [%s]",
-                                                                switch_str_nil(handle->dsn), switch_str_nil(err_str));
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "The sql server is not responding for DSN %s [%s]\n",
-                                                 switch_str_nil(handle->dsn), switch_str_nil(err_str));
</del><ins>+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Failure-Message", "The sql server is not responding for DSN %s [%s][%d]",
+                                                                switch_str_nil(handle->dsn), switch_str_nil(err_str), code);
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "The sql server is not responding for DSN %s [%s][%d]\n",
+                                                 switch_str_nil(handle->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, "Additional-Info", "The connection has been re-established");
</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->env == SQL_NULL_HANDLE) {
+                result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &handle->env);
+
+                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHandle\n");
+                        return SWITCH_ODBC_FAIL;
+                }
+
+                result = SQLSetEnvAttr(handle->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
+
+                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error SetEnv\n");
+                        SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
+                        return SWITCH_ODBC_FAIL;
+                }
+
+                result = SQLAllocHandle(SQL_HANDLE_DBC, handle->env, &handle->con);
+
+                if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error AllocHDB %d\n", result);
+                        SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
+                        return SWITCH_ODBC_FAIL;
+                }
+                SQLSetConnectAttr(handle->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
+        }
+        if (handle->state == SWITCH_ODBC_STATE_CONNECTED) {
+                switch_odbc_handle_disconnect(handle);
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Re-connecting %s\n", handle->dsn);
+        }
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting %s\n", handle->dsn);
+
+        if (!strstr(handle->dsn, "DRIVER")) {
+                result = SQLConnect(handle->con, (SQLCHAR *) handle->dsn, SQL_NTS, (SQLCHAR *) handle->username, SQL_NTS, (SQLCHAR *) handle->password, SQL_NTS);
+        } else {
+                SQLCHAR outstr[1024] = { 0 };
+                SQLSMALLINT outstrlen = 0;
+                result =
+                        SQLDriverConnect(handle->con, NULL, (SQLCHAR *) handle->dsn, (SQLSMALLINT) strlen(handle->dsn), outstr, sizeof(outstr), &outstrlen,
+                                                         SQL_DRIVER_NOPROMPT);
+        }
+
+        if ((result != SQL_SUCCESS) && (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, "%s\n", err_str);
+                        free(err_str);
+                } else {
+                        SQLGetDiagRec(SQL_HANDLE_DBC, handle->con, 1, stat, &err, msg, 100, &mlen);
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error SQLConnect=%d errno=%d %s\n", result, (int) err, msg);
+                }
+                SQLFreeHandle(SQL_HANDLE_ENV, handle->env);
+                return SWITCH_ODBC_FAIL;
+        }
+
+        result = SQLGetInfo(handle->con, SQL_DRIVER_NAME, (SQLCHAR *) handle->odbc_driver, 255, &valueLength);
+        if (result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO) {
+                for (i = 0; i < valueLength; ++i)
+                        handle->odbc_driver[i] = (char) toupper(handle->odbc_driver[i]);
+        }
+
+        if (strstr(handle->odbc_driver, "FIREBIRD") != 0 || strstr(handle->odbc_driver, "FB32") != 0 || strstr(handle->odbc_driver, "FB64") != 0) {
+                handle->is_firebird = TRUE;
+        } else {
+                handle->is_firebird = FALSE;
+        }
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connected to [%s]\n", handle->dsn);
+        handle->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>