onur
onur

Reputation: 6375

How can I check database is up or down without connection?

I use oledbconnection like this:

try
{
    OleDbConnection Conn;
    using (Conn = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE1:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME"))
        {
            Conn.Open();
            v1 = 1;
            Conn.Close();
        }
}
catch (Exception)
{
    v1 = 0;
}

And use v1 on panel like this:

if (v1 == 1)
{
    Panel1.BackColor = System.Drawing.Color.Yellow;
}
if (v1 == 0)
{
    Panel1.BackColor = System.Drawing.Color.Red;
}

It is simple and work for me. When I open this page, connection open and close, panel color be yellow or red.

But I want to check more than 20 database like this. When I open this page, 20 connection open, and page waiting so much. And of course every time database connection opening. I can use outputcache, but I need to check every 30 seconds( I use html refresh on this page ).

How can I do that with other way? Can I use tnsping for this? If yes, how can I use tnsping for database.

(Ping does not allow on this servers and I don't want to check server is up or not, I want to check db is up or not)

Upvotes: 0

Views: 1339

Answers (3)

Dennis
Dennis

Reputation: 37770

How can I do that with other way?

There are no general-purpose way to check availability of network resource. There could be some indirect ways (like ICMP, socket availability, etc), but they guarantee nothing. The right way to go is to try to connect to resource (database in your case).

But I want to check more than 20 database like this. When I open this page, 20 connection open, and page waiting so much

It looks like architecture issue.

If this page is something like infrastructure monitoring software, I'd write a regular (Windows) service as a back-end, which will periodically pull databases in parallel (using async/await is a best choice here, IMO).

Then, web front-end should pull this service to get results. Since results could be stored either in local database, or in memory, response time will be rather fast (it depends on how service and web application will communicate).

Upvotes: 2

Rahul
Rahul

Reputation: 77876

Simple way of checking it is connecting to the database. if the server is not up then it will end up throwing a SqlException and you can check the Error property of the exception object which is a collection of sqlError object.

Upvotes: 2

danish
danish

Reputation: 5610

You will need to connect to database in order to check whether it is up or not. tnsping only checks if socket is available. There is no guarantee that the database you are trying to connect is up or not.

IMHO, you can run multiple threads and connect in parallel. You can then catch the proper exceptions and get proper information in case database is not accessible.

Upvotes: 2

Related Questions