diff src/mysql.sml @ 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
line wrap: on
line diff
--- 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,