aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/c/urweb.c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2015-10-17 10:49:25 -0400
committerGravatar Adam Chlipala <adam@chlipala.net>2015-10-17 10:49:25 -0400
commit2d198c091036767fdad1c3cd79f1bba4664682ea (patch)
tree864c452ff007b190575e7561bf94f389e833b1f7 /src/c/urweb.c
parentd8dc62e905f11344720996fa8f38ead7dd6158e8 (diff)
Start of support for surviving database-server restarts, for Postgres
Diffstat (limited to 'src/c/urweb.c')
-rw-r--r--src/c/urweb.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 6d3836f1..4ce469bd 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -797,10 +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");
+}
+
+int uw_try_reconnecting_if_at_most_one(uw_context ctx) {
+ if (ctx->at_most_one_query) {
+ uw_try_reconnecting(ctx);
+ return 1;
+ } else
+ return 0;
+}
+
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;
}
}