[svn-commits] branch 1.2 r257 - in /branches/1.2: app_addon_sql_mysql.c res_config_mysql.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Sun Jul 9 21:13:07 MST 2006


Author: tilghman
Date: Sun Jul  9 23:13:07 2006
New Revision: 257

URL: http://svn.digium.com/view/asterisk-addons?rev=257&view=rev
Log:
Bug 7410 - Fix mysql reconnection on mysql client version > 5.0.3

Modified:
    branches/1.2/app_addon_sql_mysql.c
    branches/1.2/res_config_mysql.c

Modified: branches/1.2/app_addon_sql_mysql.c
URL: http://svn.digium.com/view/asterisk-addons/branches/1.2/app_addon_sql_mysql.c?rev=257&r1=256&r2=257&view=diff
==============================================================================
--- branches/1.2/app_addon_sql_mysql.c (original)
+++ branches/1.2/app_addon_sql_mysql.c Sun Jul  9 23:13:07 2006
@@ -178,20 +178,20 @@
 	}
 }
 
-static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
-	if( id>=0 ) {
-		char s[100] = "";
-		snprintf(s, sizeof(s)-1, "%d", id);
+static int set_asterisk_int(struct ast_channel *chan, const char *varname, int id) {
+	if (id >= 0) {
+		char s[13] = "";
+		snprintf(s, sizeof(s), "%d", id);
 #if EXTRA_LOG
-		ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
+		ast_log(LOG_WARNING, "MYSQL: setting var '%s' to value '%s'\n", varname, s);
 #endif
-		pbx_builtin_setvar_helper(chan,varname,s);
+		pbx_builtin_setvar_helper(chan, varname, s);
 	}
 	return id;
 }
 
 static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data) {
-	return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
+	return set_asterisk_int(chan, varname, add_identifier(identifier_type, data));
 }
 
 static int safe_scan_int( char** data, char* delim, int def ) {
@@ -227,12 +227,11 @@
 	if( connid_var && dbhost && dbuser && dbpass && dbname ) {
 		mysql = mysql_init(NULL);
 		if (mysql) {
-			if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
-				add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
+			if (mysql_real_connect(mysql, dbhost, dbuser, dbpass, dbname, 0, NULL, 0)) {
+				add_identifier_and_set_asterisk_int(chan, connid_var, AST_MYSQL_ID_CONNID, mysql);
 				return 0;
-			}
-			else {
-				ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
+			} else {
+				ast_log(LOG_WARNING, "mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n", dbhost, dbuser, dbname);
 			}
 		}
 		else {
@@ -416,8 +415,26 @@
 
 int unload_module(void)
 {
+	int res;
+	struct ast_MYSQL_id *i;
+	struct MYSQLidshead *headp = &_mysql_ids_head;
+
 	STANDARD_HANGUP_LOCALUSERS;
-	return ast_unregister_application(app);
+	res = ast_unregister_application(app);
+
+	/* Dispose of any ids left hanging */
+	AST_LIST_LOCK(headp);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(headp, i, entries) {
+		if (i->identifier_type == AST_MYSQL_ID_RESID)
+			mysql_free_result(i->identifier);
+		else if (i->identifier_type == AST_MYSQL_ID_CONNID)
+			mysql_close(i->identifier);
+		AST_LIST_REMOVE_CURRENT(headp, entries);
+		free(i);
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(headp);
+	return res;
 }
 
 int load_module(void)

Modified: branches/1.2/res_config_mysql.c
URL: http://svn.digium.com/view/asterisk-addons/branches/1.2/res_config_mysql.c?rev=257&r1=256&r2=257&view=diff
==============================================================================
--- branches/1.2/res_config_mysql.c (original)
+++ branches/1.2/res_config_mysql.c Sun Jul  9 23:13:07 2006
@@ -47,6 +47,7 @@
 #include <asterisk/cli.h>
 #include <asterisk/utils.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <mysql.h>
 #include <mysql_version.h>
@@ -592,6 +593,9 @@
 static int mysql_reconnect(const char *database)
 {
 	char my_database[50];
+#ifdef MYSQL_OPT_RECONNECT
+	my_bool trueval = 1;
+#endif
 
 	if(!database || ast_strlen_zero(database))
 		ast_copy_string(my_database, dbname, sizeof(my_database));
@@ -600,6 +604,7 @@
 
 	/* mutex lock should have been locked before calling this function. */
 
+reconnect_tryagain:
 	if((!connected) && (dbhost || dbsock) && dbuser && dbpass && my_database) {
 		if(!mysql_init(&mysql)) {
 			ast_log(LOG_WARNING, "MySQL RealTime: Insufficient memory to allocate MySQL resource.\n");
@@ -607,29 +612,36 @@
 			return 0;
 		}
 		if(mysql_real_connect(&mysql, dbhost, dbuser, dbpass, my_database, dbport, dbsock, 0)) {
+#ifdef MYSQL_OPT_RECONNECT
+			/* The default is no longer to automatically reconnect on failure,
+			 * (as of 5.0.3) so we have to set that option here. */
+			mysql_options(&mysql, MYSQL_OPT_RECONNECT, &trueval);
+#endif
 			ast_log(LOG_DEBUG, "MySQL RealTime: Successfully connected to database.\n");
 			connected = 1;
 			connect_time = time(NULL);
 			return 1;
 		} else {
-			ast_log(LOG_ERROR, "MySQL RealTime: Failed to connect database server %s on %s. Check debug for more info.\n", dbname, dbhost);
-			ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: %s\n", mysql_error(&mysql));
+			ast_log(LOG_ERROR, "MySQL RealTime: Failed to connect database server %s on %s (err %d). Check debug for more info.\n", dbname, dbhost, mysql_errno(&mysql));
+			ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect (%d): %s\n", mysql_errno(&mysql), mysql_error(&mysql));
 			connected = 0;
 			return 0;
 		}
 	} else {
-		if(mysql_ping(&mysql) != 0) {
+		/* MySQL likes to return an error, even if it reconnects successfully.
+		 * So the postman pings twice. */
+		if (mysql_ping(&mysql) != 0 && mysql_ping(&mysql) != 0) {
 			connected = 0;
-			ast_log(LOG_ERROR, "MySQL RealTime: Failed to reconnect. Check debug for more info.\n");
-			ast_log(LOG_DEBUG, "MySQL RealTime: Server Error: %s\n", mysql_error(&mysql));
-			return 0;
+			ast_log(LOG_ERROR, "MySQL RealTime: Ping failed (%d).  Trying an explicit reconnect.\n", mysql_errno(&mysql));
+			ast_log(LOG_DEBUG, "MySQL RealTime: Server Error (%d): %s\n", mysql_errno(&mysql), mysql_error(&mysql));
+			goto reconnect_tryagain;
 		}
 
 		connected = 1;
 
 		if(mysql_select_db(&mysql, my_database) != 0) {
-			ast_log(LOG_WARNING, "MySQL RealTime: Unable to select database: %s. Still Connected.\n", my_database);
-			ast_log(LOG_DEBUG, "MySQL RealTime: Database Select Failed: %s\n", mysql_error(&mysql));
+			ast_log(LOG_WARNING, "MySQL RealTime: Unable to select database: %s. Still Connected (%d).\n", my_database, mysql_errno(&mysql));
+			ast_log(LOG_DEBUG, "MySQL RealTime: Database Select Failed (%d): %s\n", mysql_error(&mysql), mysql_errno(&mysql));
 			return 0;
 		}
 



More information about the svn-commits mailing list