summaryrefslogtreecommitdiff
path: root/src/c/urweb.c
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@mit.edu>2015-10-24 11:06:31 -0400
committerGravatar Benjamin Barenblat <bbaren@mit.edu>2015-10-24 11:06:31 -0400
commit7d861ea0debf944cb8e3e38d73a8c0197de574b3 (patch)
treeed87d21ffe040bf296d2230c7490abda8e34752c /src/c/urweb.c
parent598756dc69f89cf2dd2b889cad63a7a690ae7ed7 (diff)
parentcb0109804fdad0dd423bb344446344cdc08c0886 (diff)
Merge branch 'upstream' into dfsg_clean20151018+dfsg
Diffstat (limited to 'src/c/urweb.c')
-rw-r--r--src/c/urweb.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 6d3836f1..d656ae03 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -797,12 +797,37 @@ failure_kind uw_begin(uw_context ctx, char *path) {
return r;
}
+static void uw_try_reconnecting(uw_context ctx) {
+ // Hm, error starting transaction.
+ // Maybe the database server died but has since come back up.
+ // Let's try starting from scratch.
+ if (ctx->db) {
+ ctx->app->db_close(ctx);
+ ctx->db = NULL;
+ }
+ ctx->app->db_init(ctx);
+
+ if (!ctx->db)
+ uw_error(ctx, FATAL, "Error reopening database connection");
+}
+
+void uw_try_reconnecting_and_restarting(uw_context ctx) {
+ uw_try_reconnecting(ctx);
+ uw_error(ctx, BOUNDED_RETRY, "Restarting transaction after fixing database connection");
+}
+
void uw_ensure_transaction(uw_context ctx) {
if (!ctx->transaction_started && !ctx->at_most_one_query) {
- if (ctx->app->db_begin(ctx, ctx->could_write_db))
- uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
+ if (!ctx->db || ctx->app->db_begin(ctx, ctx->could_write_db)) {
+ uw_try_reconnecting(ctx);
+
+ if (ctx->app->db_begin(ctx, ctx->could_write_db))
+ uw_error(ctx, FATAL, "Error running SQL BEGIN");
+ }
+
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) {