comparison src/c/urweb.c @ 2176:d2a98983f502

Start of support for surviving database-server restarts, for Postgres
author Adam Chlipala <adam@chlipala.net>
date Sat, 17 Oct 2015 10:49:25 -0400
parents 8a01e8f21de9
children 00cf8214c2e3
comparison
equal deleted inserted replaced
2175:3ffef52d549c 2176:d2a98983f502
795 ctx->app->handle(ctx, path); 795 ctx->app->handle(ctx, path);
796 796
797 return r; 797 return r;
798 } 798 }
799 799
800 static void uw_try_reconnecting(uw_context ctx) {
801 // Hm, error starting transaction.
802 // Maybe the database server died but has since come back up.
803 // Let's try starting from scratch.
804 if (ctx->db) {
805 ctx->app->db_close(ctx);
806 ctx->db = NULL;
807 }
808 ctx->app->db_init(ctx);
809
810 if (!ctx->db)
811 uw_error(ctx, FATAL, "Error reopening database connection");
812 }
813
814 int uw_try_reconnecting_if_at_most_one(uw_context ctx) {
815 if (ctx->at_most_one_query) {
816 uw_try_reconnecting(ctx);
817 return 1;
818 } else
819 return 0;
820 }
821
800 void uw_ensure_transaction(uw_context ctx) { 822 void uw_ensure_transaction(uw_context ctx) {
801 if (!ctx->transaction_started && !ctx->at_most_one_query) { 823 if (!ctx->transaction_started && !ctx->at_most_one_query) {
802 if (ctx->app->db_begin(ctx, ctx->could_write_db)) 824 if (!ctx->db || ctx->app->db_begin(ctx, ctx->could_write_db)) {
803 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN"); 825 uw_try_reconnecting(ctx);
826
827 if (ctx->app->db_begin(ctx, ctx->could_write_db))
828 uw_error(ctx, FATAL, "Error running SQL BEGIN");
829 }
830
804 ctx->transaction_started = 1; 831 ctx->transaction_started = 1;
805 } 832 }
806 } 833 }
807 834
808 uw_Basis_client uw_Basis_self(uw_context ctx) { 835 uw_Basis_client uw_Basis_self(uw_context ctx) {