]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
expand per-thread db caching to odbc
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 12 Nov 2009 19:54:50 +0000 (19:54 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 12 Nov 2009 19:54:50 +0000 (19:54 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15453 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/endpoints/mod_sofia/mod_sofia.c
src/mod/endpoints/mod_sofia/mod_sofia.h
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_glue.c
src/mod/endpoints/mod_sofia/sofia_reg.c
src/switch_odbc.c

index 6c9f2a4310732546dec6f5c6129a180f0e2807e0..e89735169d1e244e00b747a84db3f49c577f4656 100644 (file)
@@ -3537,9 +3537,10 @@ static void general_event_handler(switch_event_t *event)
                {
                        const char *cond = switch_event_get_header(event, "condition");
 
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
 
                        if (cond && !strcmp(cond, "network-address-change") && mod_sofia_globals.auto_restart) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
+
                                const char *old_ip4 = switch_event_get_header_nil(event, "network-address-previous-v4");
                                const char *new_ip4 = switch_event_get_header_nil(event, "network-address-change-v4");
                                const char *old_ip6 = switch_event_get_header_nil(event, "network-address-previous-v6");
index e6c5e958f84bef1f899560c0360ce443b49e6101..638400b452fd4b0bc349ad6e41e4f3b5b7fb529f 100644 (file)
@@ -36,7 +36,8 @@
 
 /*Defines etc..*/
 /*************************************************************************************************************************************************************/
-#define MANUAL_BYE
+#define MANUAL_BYE 1
+#define SQL_CACHE_TIMEOUT 300
 #define DEFAULT_NONCE_TTL 60
 #define IREG_SECONDS 30
 #define GATEWAY_SECONDS 1
@@ -489,7 +490,7 @@ struct sofia_profile {
        char *odbc_dsn;
        char *odbc_user;
        char *odbc_pass;
-       switch_odbc_handle_t *master_odbc;
+       //      switch_odbc_handle_t *master_odbc;
        switch_queue_t *sql_queue;
        char *acl[SOFIA_MAX_ACL];
        uint32_t acl_count;
@@ -673,7 +674,10 @@ typedef struct {
 
 typedef struct {
        switch_core_db_t *db;
+       switch_odbc_handle_t *odbc_dbh;
        time_t last_used;
+       switch_mutex_t *mutex;
+       switch_memory_pool_t *pool;
 } sofia_cache_db_handle_t;
 
 #define sofia_test_pflag(obj, flag) ((obj)->pflags[flag] ? 1 : 0)
@@ -797,7 +801,7 @@ void sofia_presence_set_chat_hash(private_object_t *tech_pvt, sip_t const *sip);
 switch_status_t sofia_on_hangup(switch_core_session_t *session);
 char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup);
 void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip);
-void sofia_glue_sql_close(sofia_profile_t *profile);
+void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune);
 int sofia_glue_init_sql(sofia_profile_t *profile);
 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);
 switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
index ba08175299824089d507767efc5c164ae9f51fdc..8e05059c7cc1c4a21834a4e10e7a9de612f29a50 100644 (file)
@@ -960,7 +960,9 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread
                
                if (++loops >= 1000) {
                        if (++ireg_loops >= IREG_SECONDS) {
-                               sofia_reg_check_expire(profile, switch_epoch_time_now(NULL), 0);
+                               time_t now = switch_epoch_time_now(NULL);
+                               sofia_reg_check_expire(profile, now, 0);
+                               sofia_glue_sql_close(profile, now);
                                ireg_loops = 0;
                        }
 
@@ -1261,7 +1263,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
                }
        }
 
-       sofia_glue_sql_close(profile);
+       sofia_glue_sql_close(profile, 0);
        su_home_unref(profile->home);
        su_root_destroy(profile->s_root);
        pool = profile->pool;
index d34272cdb363da92c7d87abc0dcbcd5ae1c9e958..006eba57ac6879140c353fad583da3ac85f0e6c5 100644 (file)
@@ -3610,6 +3610,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
 {
        char *test_sql = NULL;
        switch_core_db_t *db = NULL;
+       switch_odbc_handle_t *odbc_dbh = NULL;
 
        char reg_sql[] =
                "CREATE TABLE sip_registrations (\n"
@@ -3769,14 +3770,17 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                        NULL    
                };
 
-               if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
+
+               if (!(odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
                        return 0;
                }
-               if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
+
+               if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_ODBC_SUCCESS) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Connecting ODBC DSN: %s\n", profile->odbc_dsn);
+                       switch_odbc_handle_destroy(&odbc_dbh);
                        return 0;
                }
-
+               
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
                
                test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
@@ -3785,9 +3789,9 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                                                                  "like '%%' and mwi_user  like '%%' and mwi_host like '%%'", 
                                                                  mod_sofia_globals.hostname);
 
-               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);
+               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);
                }
                free(test_sql);
 
@@ -3795,55 +3799,55 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and network_ip like '%%' and network_port like '%%'", 
                                                                  mod_sofia_globals.hostname);
 
-               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);
+               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);
                }
 
                free(test_sql);
                test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and contact like '%%'", mod_sofia_globals.hostname);
 
-               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);
+               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);
                }
 
                test_sql = switch_mprintf("delete from sip_presence where hostname='%q'", mod_sofia_globals.hostname);
 
-               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);
+               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);
                }
 
                free(test_sql);
                test_sql = switch_mprintf("delete from sip_authentication where hostname='%q' or last_nc >= 0", mod_sofia_globals.hostname);
 
-               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);
+               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);
                }
                free(test_sql);
 
                test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where contact_str='' or hostname='%q' and network_ip like '%%'",
                                                                  mod_sofia_globals.hostname);
-               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);
+               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);
                }
                free(test_sql);
 
 
                test_sql = switch_mprintf("delete from sip_shared_appearance_dialogs where contact_str='' or hostname='%q' and network_ip like '%%'",
                                                                  mod_sofia_globals.hostname);
-               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);
+               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);
                }
                free(test_sql);
 
 
                for (x = 0; indexes[x]; x++) {
-                       switch_odbc_handle_exec(profile->master_odbc, indexes[x], NULL);
+                       switch_odbc_handle_exec(odbc_dbh, indexes[x], NULL);
                }
 
 
@@ -3952,44 +3956,73 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                switch_core_db_exec(db, "create index if not exists sa_hostname on sip_authentication (hostname)", NULL, NULL, NULL);
        }
 
-       if (switch_odbc_available() && profile->odbc_dsn) {
-               return profile->master_odbc ? 1 : 0;
+       if (odbc_dbh) {
+               switch_odbc_handle_destroy(&odbc_dbh);
+               return 1;
        }
-       
+
        if (db) {
                switch_core_db_close(db);
                return 1;
-       } else {
-               return 0;
        }
 
+       return 0;       
+
 }
 
-void sofia_glue_sql_close(sofia_profile_t *profile)
+void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune)
 {
-       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;
-
-               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);
+       switch_hash_index_t *hi;
+       const void *var;
+       void *val;
+       sofia_cache_db_handle_t *dbh = NULL;
+       int locked = 0;
+       char *key;
+
+       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;
+
+               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);
                                goto top;
+       
+                       } else {
+                               if (!prune) locked++;
+                               continue;
                        }
                }
-               switch_mutex_unlock(profile->ireg_mutex);
+       }
 
+       if (locked) {
+               goto top;
        }
+
+       switch_mutex_unlock(profile->ireg_mutex);
 }
 
 void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic)
@@ -4026,45 +4059,85 @@ void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t
        }
 }
 
-switch_core_db_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
+
+void sofia_glue_release_db_handle(sofia_cache_db_handle_t **dbh)
+{
+       if (dbh && *dbh) {
+               switch_mutex_unlock((*dbh)->mutex);
+               *dbh = NULL;
+       }
+}
+
+sofia_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
 {
        switch_thread_id_t self = switch_thread_self();
        char thread_str[256] = "";
-       switch_core_db_t *db = NULL;
        sofia_cache_db_handle_t *dbh = NULL;
-
+       
        snprintf(thread_str, sizeof(thread_str) - 1, "%lu", (unsigned long)(intptr_t)self);
 
        switch_mutex_lock(profile->ireg_mutex);
-       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);
+       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);
+
                switch_core_hash_insert(profile->db_hash, thread_str, dbh);
-               db = dbh->db;
        }
+
+ end:
+
+       if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
+
        switch_mutex_unlock(profile->ireg_mutex);
 
-       return db;
+       return dbh;
 }
 
 
 
 void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
 {
-       switch_core_db_t *db;
+       sofia_cache_db_handle_t *dbh = NULL;
 
        if (mutex) {
                switch_mutex_lock(mutex);
        }
 
-       if (switch_odbc_available() && profile->odbc_dsn) {
+       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) {
                switch_odbc_statement_handle_t stmt;
-               if (switch_odbc_handle_exec(profile->master_odbc, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
+               if (switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
                        char *err_str;
-                       err_str = switch_odbc_handle_get_error(profile->master_odbc, stmt);
+                       err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
                        if (!zstr(err_str)) {
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, err_str);
                        }
@@ -4076,21 +4149,19 @@ void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch
        } else {
                char *errmsg;
 
-               
-               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, NULL, NULL, &errmsg);
+        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);
-               }
+        if (errmsg) {
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
+            switch_core_db_free(errmsg);
+        }
+               
        }
 
   end:
+
+       sofia_glue_release_db_handle(&dbh);
+
        if (mutex) {
                switch_mutex_unlock(mutex);
        }
@@ -4100,27 +4171,24 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
                                                                                          switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
 {
        switch_bool_t ret = SWITCH_FALSE;
-       switch_core_db_t *db;
        char *errmsg = NULL;
-
+       sofia_cache_db_handle_t *dbh = NULL;
+       
        if (mutex) {
                switch_mutex_lock(mutex);
        }
 
+       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() && profile->odbc_dsn) {
-               switch_odbc_handle_callback_exec(profile->master_odbc, sql, callback, pdata);
+       if (switch_odbc_available() && dbh->odbc_dbh) {
+               switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata);
        } else if (profile->odbc_dsn) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
        } else {
-
-               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);
+               switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
 
                if (errmsg) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
@@ -4129,46 +4197,43 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
        }
 
   end:
-       if (mutex) {
-               switch_mutex_unlock(mutex);
-       }
-       return ret;
-}
 
-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;
+       sofia_glue_release_db_handle(&dbh);
 
-       if (switch_odbc_handle_exec_string(profile->master_odbc, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
-               ret = resbuf;
+       if (mutex) {
+               switch_mutex_unlock(mutex);
        }
 
        return ret;
 }
 
-
 char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
 {
-       switch_core_db_t *db;
        switch_core_db_stmt_t *stmt;
        char *ret = NULL;
 
-       if (switch_odbc_available() && profile->odbc_dsn) {
-               return sofia_glue_execute_sql2str_odbc(profile, mutex, sql, resbuf, len);
-       }
+       sofia_cache_db_handle_t *dbh = NULL;
+
+       if (!(dbh = sofia_glue_get_db_handle(profile))) {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+        goto end;
+    }
 
        if (mutex) {
                switch_mutex_lock(mutex);
        }
 
-       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;
+       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;
+               }
        }
 
-       if (switch_core_db_prepare(db, sql, -1, &stmt, 0)) {
+       if (switch_core_db_prepare(dbh->db, sql, -1, &stmt, 0)) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
-               goto fail;
+               goto end;
        } else {
                int running = 1;
                int colcount;
@@ -4183,7 +4248,7 @@ char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex
                                                switch_copy_string(resbuf, (char *) txt, len);
                                                ret = resbuf;
                                        } else {
-                                               goto fail;
+                                               goto end;
                                        }
                                }
                                break;
@@ -4198,14 +4263,14 @@ char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex
                switch_core_db_finalize(stmt);
        }
 
-  fail:
-       switch_core_db_close(db);
+ end:
 
-  end:
        if (mutex) {
                switch_mutex_unlock(mutex);
        }
 
+       sofia_glue_release_db_handle(&dbh);
+
        return ret;
 }
 
index 6510796ac0e0814fa492b1ce8337ef0a32d3e64f..d12385f7b3b98686db7888458d9c1929a9b5ea01 100644 (file)
@@ -538,13 +538,6 @@ void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot)
 {
        char sql[1024];
 
-       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;
-               }
-       }
-
        switch_mutex_lock(profile->ireg_mutex);
 
        if (now) {
index cb0a91c801b562a7f683e697f2c99e901f42ce8f..121cc529a0340953a0af49f0a00a1cd18a12c6f4 100644 (file)
@@ -130,90 +130,6 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_disconnect(switch_odbc_h
 #endif
 }
 
-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
-}
 
 #ifdef SWITCH_HAVE_ODBC
 static int db_is_up(switch_odbc_handle_t *handle)
@@ -227,10 +143,11 @@ static int db_is_up(switch_odbc_handle_t *handle)
        char *err_str = NULL;
        SQLCHAR sql[255] = "";
        int max_tries = 120;
-
+       int code = 0;
        SQLRETURN rc;
        SQLSMALLINT nresultcols;
 
+
   top:
 
        if (!handle) {
@@ -245,28 +162,33 @@ static int db_is_up(switch_odbc_handle_t *handle)
        }
 
        if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) {
+               code = __LINE__;
                goto error;
        }
 
        if (SQLPrepare(stmt, sql, SQL_NTS) != SQL_SUCCESS) {
+               code = __LINE__;
                goto error;
        }
 
        result = SQLExecute(stmt);
 
        if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO) {
+               code = __LINE__;
                goto error;
        }
-
+       
        SQLRowCount (stmt, &m);
        rc = SQLNumResultCols (stmt, &nresultcols);
        if (rc != SQL_SUCCESS){
+               code = __LINE__;
                goto error;
        }
        ret = (int) nresultcols;
        /* determine statement type */
        if (nresultcols <= 0) {
                /* statement is not a select statement */
+               code = __LINE__;
                goto error;
        }
 
@@ -279,10 +201,10 @@ static int db_is_up(switch_odbc_handle_t *handle)
        max_tries--;
 
        if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
-               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));
+               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);
 
                if (recon == SWITCH_ODBC_SUCCESS) {
                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Additional-Info", "The connection has been re-established");
@@ -334,6 +256,91 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_statement_handle_free(switch_od
 }
 
 
+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
+}
+
 SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_exec_string(switch_odbc_handle_t *handle,
                                                                                                                                        char *sql,
                                                                                                                                        char *resbuf,