diff options
author | 2015-10-19 14:42:22 -0400 | |
---|---|---|
committer | 2015-10-19 14:42:22 -0400 | |
commit | 9d12f9199103b3b98b3e3990bbf586ee0fd34130 (patch) | |
tree | a7863db3f0823fa648f3af83da11fe081aea48d5 /src/c | |
parent | f425194d947691ceeaad9ec73fdc7c2c176ebfe3 (diff) | |
parent | 3256142dc01f7f173289a1910e726b662a877408 (diff) |
Merge.
Diffstat (limited to 'src/c')
-rw-r--r-- | src/c/urweb.c | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c index 957f158c..cc0eab06 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -809,12 +809,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) { |