changeset 2178:c76a9712334c

Database-server reconnection for MySQL
author Adam Chlipala <adam@chlipala.net>
date Sat, 17 Oct 2015 11:31:22 -0400
parents 00cf8214c2e3
children 4f4ae5c92434
files src/c/urweb.c src/mysql.sml
diffstat 2 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/urweb.c	Sat Oct 17 11:08:12 2015 -0400
+++ b/src/c/urweb.c	Sat Oct 17 11:31:22 2015 -0400
@@ -813,7 +813,7 @@
 
 void uw_try_reconnecting_and_restarting(uw_context ctx) {
   uw_try_reconnecting(ctx);
-  uw_error(ctx, UNLIMITED_RETRY, "Restarting transaction after fixing database connection");
+  uw_error(ctx, BOUNDED_RETRY, "Restarting transaction after fixing database connection");
 }
 
 void uw_ensure_transaction(uw_context ctx) {
@@ -826,7 +826,8 @@
     }
 
     ctx->transaction_started = 1;
-  }
+  } else if (ctx->at_most_one_query && !ctx->db)
+    uw_try_reconnecting(ctx);
 }
 
 uw_Basis_client uw_Basis_self(uw_context ctx) {
--- a/src/mysql.sml	Sat Oct 17 11:08:12 2015 -0400
+++ b/src/mysql.sml	Sat Oct 17 11:31:22 2015 -0400
@@ -861,11 +861,17 @@
                                           end) cols,
          newline,
 
-         string "if (mysql_stmt_reset(stmt)) uw_error(ctx, FATAL, \"",
-         string (ErrorMsg.spanToString loc),
-         string ": Error reseting statement: %s\\n%s\", ",
-         query,
-         string ", mysql_error(conn->conn));",
+         string "if (mysql_stmt_reset(stmt)) {",
+         box [newline,
+              string "if (mysql_errno(conn->conn) == 2006) uw_try_reconnecting_and_restarting(ctx);",
+              newline,
+              string "uw_error(ctx, FATAL, \"",
+              string (ErrorMsg.spanToString loc),
+              string ": Error reseting statement: %s\\n%s\", ",
+              query,
+              string ", mysql_error(conn->conn));",
+              newline],
+         string "}",
          newline,
          newline,
 
@@ -1233,7 +1239,9 @@
 
 fun dmlCommon {loc, dml, mode} =
     box [string "if (mysql_stmt_execute(stmt)) {",
-         box [string "if (mysql_errno(conn->conn) == 1213)",
+         box [string "if (mysql_errno(conn->conn) == 2006) uw_try_reconnecting_and_restarting(ctx);",
+              newline,
+              string "if (mysql_errno(conn->conn) == 1213)",
               newline,
               box [string "uw_error(ctx, UNLIMITED_RETRY, \"Deadlock detected\");",
                    newline],
@@ -1540,7 +1548,13 @@
          newline,
          newline,
 
-         string "if (mysql_query(conn->conn, insert)) uw_error(ctx, FATAL, \"'nextval' INSERT failed\");",
+         string "if (mysql_query(conn->conn, insert)) {",
+         box [newline,
+              string "if (mysql_errno(conn->conn) == 2006) uw_try_reconnecting_and_restarting(ctx);",
+              newline,
+              string "uw_error(ctx, FATAL, \"'nextval' INSERT failed\");",
+              newline],
+         string "}",
          newline,
          string "n = mysql_insert_id(conn->conn);",
          newline,