Index: res/res_odbc.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_odbc.c,v
retrieving revision 1.11
diff -u -r1.11 res_odbc.c
--- res/res_odbc.c	25 Jan 2005 06:10:20 -0000	1.11
+++ res/res_odbc.c	7 Feb 2005 22:35:44 -0000
@@ -33,6 +33,7 @@
 
 static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
 
+
 static void odbc_destroy(void)
 {
 	int x = 0;
@@ -56,7 +57,7 @@
 	return NULL;
 }
 
-static int odbc_write(struct odbc_list *registry, char *name, odbc_obj * obj)
+static int odbc_write(struct odbc_list *registry, char *name, odbc_obj *obj)
 {
 	int x = 0;
 	for (x = 0; x < MAX_ODBC_HANDLES; x++) {
@@ -81,6 +82,47 @@
 static char *tdesc = "ODBC Resource";
 /* internal stuff */
 
+
+int odbc_sanity_check(odbc_obj *obj) 
+{
+	char *test_sql = "select 1";
+	SQLHSTMT stmt;
+	int res = 0;
+	SQLLEN rowcount = 0;
+
+	ast_mutex_lock(&obj->lock);
+	if(obj->up) { /* so you say... let's make sure */
+		res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
+		if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+			obj->up = 0; /* Liar!*/
+		} else {
+			res = SQLPrepare(stmt, test_sql, SQL_NTS);
+			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+				obj->up = 0; /* Liar!*/
+			} else {
+				res = SQLExecute(stmt);
+				if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+					obj->up = 0; /* Liar!*/
+				} else {
+					res = SQLRowCount(stmt, &rowcount);
+					if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+						obj->up = 0; /* Liar!*/
+					}
+				}
+			}
+		}
+		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	}
+	ast_mutex_unlock(&obj->lock);
+
+	if(!obj->up) { /* Try to reconnect! */
+		ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
+		odbc_obj_disconnect(obj);
+		odbc_obj_connect(obj);
+	}
+	return obj->up;
+}
+
 static int load_odbc_config(void)
 {
 	static char *cfg = "res_odbc.conf";
@@ -145,9 +187,11 @@
 	return 0;
 }
 
-int odbc_dump_fd(int fd, odbc_obj * obj)
+int odbc_dump_fd(int fd, odbc_obj *obj)
 {
-	ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
+	/* make sure the connection is up before we lie to our master.*/
+	odbc_sanity_check(obj);
+	ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
 	return 0;
 }
 
@@ -244,7 +288,7 @@
 
 /* api calls */
 
-int register_odbc_obj(char *name, odbc_obj * obj)
+int register_odbc_obj(char *name, odbc_obj *obj)
 {
 	if (obj != NULL)
 		return odbc_write(ODBC_REGISTRY, name, obj);
@@ -253,7 +297,11 @@
 
 odbc_obj *fetch_odbc_obj(const char *name)
 {
-	return (odbc_obj *) odbc_read(ODBC_REGISTRY, name);
+	odbc_obj *obj = NULL;
+	if((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
+		odbc_sanity_check(obj);
+	}
+	return obj;
 }
 
 odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
@@ -295,7 +343,7 @@
 	return new;
 }
 
-void destroy_obdc_obj(odbc_obj ** obj)
+void destroy_obdc_obj(odbc_obj **obj)
 {
 	odbc_obj_disconnect(*obj);
 
@@ -311,32 +359,30 @@
 	if ((*obj)->password)
 		free((*obj)->password);
 	ast_mutex_unlock(&(*obj)->lock);
+	ast_mutex_destroy(&(*obj)->lock);
 	free(*obj);
 }
 
-odbc_status odbc_obj_disconnect(odbc_obj * obj)
+odbc_status odbc_obj_disconnect(odbc_obj *obj)
 {
 	int res;
 	ast_mutex_lock(&obj->lock);
 
-	if (obj->up) {
-		res = SQLDisconnect(obj->con);
-	} else {
-		res = -1;
-	}
+	res = SQLDisconnect(obj->con);
+
 
 	if (res == ODBC_SUCCESS) {
 		ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
-		obj->up = 0;
 	} else {
 		ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
 		obj->name, obj->dsn);
 	}
+	obj->up = 0;
 	ast_mutex_unlock(&obj->lock);
 	return ODBC_SUCCESS;
 }
 
-odbc_status odbc_obj_connect(odbc_obj * obj)
+odbc_status odbc_obj_connect(odbc_obj *obj)
 {
 	int res;
 	long int err;
@@ -378,8 +424,13 @@
 		}
 		SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
 	}
+	if(obj->up) {
+		odbc_obj_disconnect(obj);
+		ast_log(LOG_NOTICE,"Re-connecting %s\n", obj->name);
+	}
+
+	ast_log(LOG_NOTICE, "Connecting %s\n", obj->name);
 
-	ast_log(LOG_NOTICE, "Calling %p/%p\n", obj->username, obj->password);
 	res = SQLConnect(obj->con,
 		   (SQLCHAR *) obj->dsn, SQL_NTS,
 		   (SQLCHAR *) obj->username, SQL_NTS,
Index: include/asterisk/res_odbc.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/res_odbc.h,v
retrieving revision 1.3
diff -u -r1.3 res_odbc.h
--- include/asterisk/res_odbc.h	21 Jan 2005 07:06:25 -0000	1.3
+++ include/asterisk/res_odbc.h	7 Feb 2005 22:35:44 -0000
@@ -47,5 +47,6 @@
 int register_odbc_obj(char *name,odbc_obj *obj);
 odbc_obj *fetch_odbc_obj(const char *name);
 int odbc_dump_fd(int fd,odbc_obj *obj);
+int odbc_sanity_check(odbc_obj *obj);
 
 #endif
