Question

What is the best way I can test the database connection using C# and Oracle.ManagedDataAccess.Client?

Should I test before every conn.Open() to see if the server is accessible or do one test on first run?

My main problem is this: if the database server is offline, I get an exception on the webpage, this is not the best way to interact with the user.

How can I display an error page if the connection couldn't be established?

Example Data Access Layer method:

public bool IsEmailAddressExisting(string emailAddr)
{
    using (OracleConnection conn = new OracleConnection(BasicConnection.connectionStringOracle))
    using (OracleCommand command = conn.CreateCommand())
    {
        conn.Open();
        command.CommandText = "SELECT id FROM Users WHERE email=:emailAddr";
        command.Parameters.Add(":emailAddr", OracleDbType.Varchar2).Value = emailAddr;

        var result = command.ExecuteScalar();

        return (result != null);
    }
}

Note: I don't want to put try-catch around EVERY method's connection.

I found this answer really useful.

Was it helpful?

Solution

What is the best way I can test the database connection using C# and Oracle.ManagedDataAccess.Client?

Not to test at all. Unless you really need this. The situation you described doesn't seem to require a test of connection.

Should I test before every conn.Open() to see if the server is accessible or do one test on first run?

No, this will end up in a code full of try-catch blocks. You can have this in some specific situations, but not in general, not for each case.

How can I display an error page if the connection couldn't be established?

Finally, this is what you really need. Exception isn't bad, it's just what it is. Database isn't available what else can you do? Well, from the point of your application you should log all unhandled exceptions and errors. But you don't need to report the exact problem to the user. You can merely display a user-friendly general error message.

Each web technology will have this. You can say something like:

Unfortunately, this functionality is not available just now. An email has been sent to the responsible person. We are looking into the problem. Please try again later.

OTHER TIPS

Note: I don't want to put try-catch around EVERY method's connection.

If you've got database connection code in a lot of methods, then that's your problem, not the database being offline now and then (though you should also get that fixed).

You should use try..catch on an high level point in your application anyway, or use the proper error handling of the web or application framework you're using, so you can display a nice "Something went wrong"-screen instead of dumping the exception to the end user.

You don't want to add a "database online"-check before executing a query, because between those two calls the database can go offline, or you can have invalid input which makes the query throw an exception anyway.

By default, OracleConnection is a connection pool. If you set the connect string attribute validateconnection to true it will do this work for you, although it will add a round trip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top