[asterisk-commits] murf: branch 1.4 r143674 - /branches/1.4/cdr/cdr_odbc.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Sep 19 16:07:06 CDT 2008


Author: murf
Date: Fri Sep 19 16:07:05 2008
New Revision: 143674

URL: http://svn.digium.com/view/asterisk?view=rev&rev=143674
Log:
This fix comes from a debugging session on a test box that has been getting hung channels when the mssqlserver bounces. All the connections then become invalid, and must be reconnected. The cdr_odbc backend had code to do it, but depended on re-establishing the connection, but re-using the STMT that had been built. By trial and error, we determined that the STMT could not be re-used after the connection was re-established. and must be rebuilt. These changes accomplish this.

Modified:
    branches/1.4/cdr/cdr_odbc.c

Modified: branches/1.4/cdr/cdr_odbc.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/cdr/cdr_odbc.c?view=diff&rev=143674&r1=143673&r2=143674
==============================================================================
--- branches/1.4/cdr/cdr_odbc.c (original)
+++ branches/1.4/cdr/cdr_odbc.c Fri Sep 19 16:07:05 2008
@@ -90,7 +90,7 @@
 	connected = 0;
 }
 
-static int odbc_log(struct ast_cdr *cdr)
+static void build_query(struct ast_cdr *cdr)
 {
 	int ODBC_res;
 	char sqlcmd[2048] = "", timestr[128];
@@ -102,7 +102,6 @@
 	else
 		ast_localtime(&cdr->start.tv_sec, &tm, NULL);
 
-	ast_mutex_lock(&odbc_lock);
 	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 	memset(sqlcmd,0,2048);
 	if (loguniqueid) {
@@ -116,13 +115,12 @@
 		"duration,billsec,disposition,amaflags,accountcode) "
 		"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
 	}
-
 	if (!connected) {
 		res = odbc_init();
 		if (res < 0) {
 			odbc_disconnect();
 			ast_mutex_unlock(&odbc_lock);
-			return 0;
+			return;
 		}				
 	}
 
@@ -134,7 +132,7 @@
 		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
 		odbc_disconnect();
 		ast_mutex_unlock(&odbc_lock);
-		return 0;
+		return;
 	}
 
 	/* We really should only have to do this once.  But for some
@@ -149,7 +147,7 @@
 		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
 		odbc_disconnect();
 		ast_mutex_unlock(&odbc_lock);
-		return 0;
+		return;
 	}
 
 	SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, &timestr, 0, NULL);
@@ -174,6 +172,14 @@
 		SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
 		SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
 	}
+}
+
+static int odbc_log(struct ast_cdr *cdr)
+{
+	int res = 0;
+
+	ast_mutex_lock(&odbc_lock);
+	build_query(cdr);
 
 	if (connected) {
 		res = odbc_do_query();
@@ -182,7 +188,7 @@
 				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
 			if (option_verbose > 10)
 				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
-			SQLDisconnect(ODBC_con);
+			odbc_disconnect();
 			res = odbc_init();
 			if (res < 0) {
 				if (option_verbose > 10)
@@ -191,6 +197,8 @@
 			} else {
 				if (option_verbose > 10)
 					ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
+				SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
+				build_query(cdr); /* what a waste. If we have to reconnect, we have to build a new query */
 				res = odbc_do_query();
 				if (res < 0) {
 					if (option_verbose > 10)
@@ -374,14 +382,11 @@
 static int odbc_do_query(void)
 {
 	int ODBC_res;
-	
 	ODBC_res = SQLExecute(ODBC_stmt);
 	
 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
 		if (option_verbose > 10)
 			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
-		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
-		odbc_disconnect();
 		return -1;
 	} else {
 		if (option_verbose > 10)




More information about the asterisk-commits mailing list