Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash Recovery in Application

What's the best way (standard solution maybe) to build crash recovery into my application so it can automatically restart on any kind of crash.

tnx.

like image 506
MBZ Avatar asked Mar 14 '11 18:03

MBZ


People also ask

What is a crash recovery?

Crash recovery is the process by which the database is moved back to a consistent and usable state. This is done by rolling back incomplete transactions and completing committed transactions that were still in memory when the crash occurred (Figure 1). Figure 1. Rolling back units of work (crash recovery)

What is recovery and types of recovery?

What Are the Types of Recovery? There are three basic types of recovery: instance recovery, crash recovery, and media recovery. Oracle performs the first two types of recovery automatically at instance startup; only media recovery requires you to issue commands.

How is the log used to recover from a crash?

The log records in the log buffer and in the log file are used to undo the changes of the failed transaction in reverse order. System (crash) recovery is needed when the whole database (transaction) system fails, e.g., due to a hardware or software error.


1 Answers

It is in general better not to do this, there's nothing pretty about a process that constantly starts and immediately crashes again with the user helplessly looking at the carnage. But I can only hand you the bullets, aiming the gun at your foot is up to you. You'll need code like this:

    static void Main(string[] args) {
        AppDomain.CurrentDomain.UnhandledException += ReportAndRestart;
        // etc..
    }

    static void ReportAndRestart(object sender, UnhandledExceptionEventArgs e) {
        string info = e.ExceptionObject.ToString();
        // Log or display info 
        //...
        // Let the user know you're restarting
        //...
        // And restart:
        System.Diagnostics.Process.Start(
            System.Reflection.Assembly.GetEntryAssembly().Location,
            string.Join(" ", Environment.GetCommandLineArgs()));
        Environment.Exit(1);
    }
}

Beware that I took a shortcut on the command line arguments. They should be quoted if they contain a path to a file that contains spaces. Don't take shortcuts on the code you're supposed to put in the ellipsis.

like image 85
Hans Passant Avatar answered Sep 30 '22 07:09

Hans Passant