开发者

help with try catch statement

开发者 https://www.devze.com 2023-03-15 23:29 出处:网络
im creating a webapplication which connects to a database and uses a ODBC connector im using the following code to do this

im creating a webapplication which connects to a database and uses a ODBC connector im using the following code to do this

private void SetupConnection()
{
    conn.ConnectionString = 
        ConfigurationManager.ConnectionStrings["live"].ConnectionString;

    OdbcDataAdapter da = 
        new OdbcDataAdapter("SELECT * FROM MTD_FIGURE_VIEW1 '", conn);

    da.Fill(ds);
}

i want to create a try catch statement which will keep on trying to connect to the database.

psudocode will be try above function, if cant connect try again if it does connect without any errors carry on.

can someone please help me on this

Question relevant update from the comments:

i just want it to keep on开发者_运维技巧 trying the reason for this is the web application im making will never switch of its constantly refreshing data, but the database is switched of fro two hours every night for backup during this period i want the app to keep on trying to connect


private void SetupConnection()
{
    conn.ConnectionString = 
        ConfigurationManager.ConnectionStrings["ZenLive"].ConnectionString;

    bool success = false;

    while(!success)
    {
        try
        {
            OdbcDataAdapter da = 
               new OdbcDataAdapter("SELECT * FROM MTD_FIGURE_VIEW1 '", conn);

            da.Fill(ds);
            success = true;
        }
        catch(Exception e)
        {
            Log(e);
            Thread.Sleep(_retryPeriod)
        }
    }
}

Note as people have commented, this is really not a good idea. You need some way to distinguish between an "expected" exception where the database is turned off and you want to keep retrying, and the case that something unexpected has happened and you don't want to keep trying.


How about,

const int MaxRetries = 42 //or any other number you like

for (int r = 1; r <= MaxRetries; r++)
{
    try
    {
        //The thing we want to test
        break;
    }
    catch (SomeExceptionWeWantToIgnore)
    {
        System.Threading.Thread.Sleep(1000) 
        //Or any number of milliseconds you like (not 0)
    }
} 

If this is somthing you would like to do many times and resuse. Youi could consider putting it in a resuable function like I did in this answer, warning this is probably overkill.


If the database isn't up there is no point in trying to connect to it. You're just wasting your time trying to connect to something that's down for backup. The I would do something like this:

DateTime backupStart = Convert.ToDateTime("20:00:00");
DateTime backupEnd = Convert.ToDateTime("22:00:00");
DateTime now = DateTime.Now;

while (true)
{
    // If its backup time then sleep for 2 hours, 0 min and 0 seconds
    if (now > backupStart && now < backupEnd)
    {
        System.Threading.Thread.Sleep(new TimeSpan(2, 0, 0));
        now = DateTime.Now; // So we dont sleep again later
    }

    try
    {
        // Try db connect
        // Break if successful
        break;
    }
    catch (Exception ex)
    {
        // Wait 30 seconds then loop then try again
        System.Threading.Thread.Sleep(new TimeSpan(0, 0, 30));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消