提交 ba2e1812 authored 作者: Anthony Minessale's avatar Anthony Minessale

expand per-thread db caching to odbc

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15453 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 a64e1a51
......@@ -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");
......
......@@ -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,
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论