diff -urN ser-0.8.14-orig/modules/mysql/con_mysql.h ser-0.8.14/modules/mysql/con_mysql.h
--- ser-0.8.14-orig/modules/mysql/con_mysql.h	2002-09-19 14:23:54.000000000 +0200
+++ ser-0.8.14/modules/mysql/con_mysql.h	2005-04-19 22:41:06.118011504 +0200
@@ -31,6 +31,7 @@
 #define CON_MYSQL_H
 
 #include <mysql/mysql.h>
+#include <mysql/errmsg.h>
 
 /*
  * MySQL specific connection data
@@ -39,12 +40,14 @@
 	MYSQL_RES* res; /* Actual result */
 	MYSQL* con;     /* Connection representation */
 	MYSQL_ROW row;  /* Actual row in the result */
+	time_t timestamp;        /* Timestamp of last query */
 };
 
 
 #define CON_RESULT(db_con)     (((struct con_mysql*)((db_con)->tail))->res)
 #define CON_CONNECTION(db_con) (((struct con_mysql*)((db_con)->tail))->con)
 #define CON_ROW(db_con)        (((struct con_mysql*)((db_con)->tail))->row)
+#define CON_TIMESTAMP(db_con)  (((struct con_mysql*)((db_con)->tail))->timestamp)
 
 
 #endif /* CON_MYSQL_H */
diff -urN ser-0.8.14-orig/modules/mysql/dbase.c ser-0.8.14/modules/mysql/dbase.c
--- ser-0.8.14-orig/modules/mysql/dbase.c	2004-04-02 18:45:10.000000000 +0200
+++ ser-0.8.14/modules/mysql/dbase.c	2005-04-20 14:31:19.897599560 +0200
@@ -38,7 +38,7 @@
 #include "dbase.h"
 #include "con_mysql.h"
 #include "db_val.h"
-
+#include "db_mod.h"
 
 static char sql_buf[SQL_BUF_LEN];
 
@@ -105,6 +105,9 @@
 
 	pkg_free(buf);
 	CON_CONNECTED(_h) = 1;
+	if(auto_reconnect) {
+		CON_CONNECTION(_h)->reconnect = 1;
+	}
 	return 0;
 }
 
@@ -138,22 +141,51 @@
  * Send an SQL query to the server
  */
 static inline int submit_query(db_con_t* _h, const char* _s)
-{	
+{
+	time_t t;
+	int i, code;	
 #ifdef PARANOID
 	if ((!_h) || (!_s)) {
 		LOG(L_ERR, "submit_query(): Invalid parameter value\n");
 		return -1;
 	}
 #endif
+
+	if (ping_interval) {
+		t = time(0);
+		if ((t - CON_TIMESTAMP(_h)) > ping_interval) {
+			if (mysql_ping(CON_CONNECTION(_h))) {
+				DBG("submit_query: mysql_ping failed\n");
+			}
+		}
+		CON_TIMESTAMP(_h) = t;
+	}
+
+
 	/* screws up the terminal when the query contains a BLOB :-( (by bogdan)
 	 * DBG("submit_query(): %s\n", _s);
 	 */
-	if (mysql_query(CON_CONNECTION(_h), _s)) {
-		LOG(L_ERR, "submit_query(): %s\n", mysql_error(CON_CONNECTION(_h)));
-		return -2;
-	} else {
-		return 0;
+		 /* When a server connection is lost and a query is attempted, most of
+  	          * the time the query will return a CR_SERVER_LOST, then at the second
+  	          * attempt to execute it, the mysql lib will reconnect and succeed.
+  	          * However is a few cases, the first attempt returns CR_SERVER_GONE_ERROR
+  	          * the second CR_SERVER_LOST and only the third succeeds.
+  	          * Thus the 3 in the loop count. Increasing the loop count over this
+  	          * value shouldn't be needed, but it doesn't hurt either, since the loop
+  	          * will most of the time stop at the second or sometimes at the third
+  	          * iteration.
+  	          */
+ 	for (i=0; i<(auto_reconnect?5:1); i++) {
+  	        if (mysql_query(CON_CONNECTION(_h), _s)==0) {
+  	        	return 0;
+		}
+  	        code = mysql_errno(CON_CONNECTION(_h));
+  	        if (code != CR_SERVER_GONE_ERROR && code != CR_SERVER_LOST) {
+  	        	break;
+  		}
 	}
+	LOG(L_ERR, "submit_query(): %s\n", mysql_error(CON_CONNECTION(_h)));
+	return -2;
 }
 
 
diff -urN ser-0.8.14-orig/modules/mysql/db_mod.c ser-0.8.14/modules/mysql/db_mod.c
--- ser-0.8.14-orig/modules/mysql/db_mod.c	2003-10-08 15:07:21.000000000 +0200
+++ ser-0.8.14/modules/mysql/db_mod.c	2005-04-19 23:30:05.460163576 +0200
@@ -36,13 +36,15 @@
 #include <stdio.h>
 #include "../../sr_module.h"
 #include "dbase.h"
+#include "db_mod.h"
 
 MODULE_VERSION
 
 
 static int mod_init(void);
 
-
+int ping_interval = 5 * 60; /* Default is 5 minutes */
+int auto_reconnect = 1;     /* Default is enabled */
 /*
  * MySQL database module interface
  */
@@ -61,13 +63,19 @@
 	{0,0,0,0,0}
 };
 
-
+/*
+ * Exported parameters
+ */
+static param_export_t params[] = {
+	{"ping_interval",	INT_PARAM, &ping_interval},
+	{"auto_reconnect", 	INT_PARAM, &auto_reconnect},
+	{0, 0, 0}
+};
 
 struct module_exports exports = {	
 	"mysql",
 	cmds,
-	0,   /*  module paramers */
-
+	params,   /*  module paramers */
 	mod_init, /* module initialization function */
 	0,        /* response function*/
 	0,        /* destroy function */
diff -urN ser-0.8.14-orig/modules/mysql/db_mod.h ser-0.8.14/modules/mysql/db_mod.h
--- ser-0.8.14-orig/modules/mysql/db_mod.h	1970-01-01 01:00:00.000000000 +0100
+++ ser-0.8.14/modules/mysql/db_mod.h	2005-04-19 23:30:11.100306144 +0200
@@ -0,0 +1,32 @@
+/* 
+ * $Id: db_mod.c,v 1.18 2003/10/08 13:07:21 janakj Exp $ 
+ *
+ * MySQL module interface
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    info@iptel.org
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+extern int auto_reconnect;
+extern int ping_interval;
+
