Test connection in Enterprise Library

  • Thread starter Thread starter Jac
  • Start date Start date
J

Jac

Hey,

How can I test if there is an connection-exception when using database
application block from the enterprise service?

I stopped the sqlserver.
I do now the following :

Database db = null;
IDataReader reader = null;
try
{
db = DatabaseFactory.CreateDatabase("Logging");
}
catch (Exception ex)
{
string e = ex.Message;
}

string sqlQuery = "SELECT * FROM log";
try
{
reader = db.ExecuteReader(CommandType.Text, sqlQuery);
}
catch (Exception ex)
{
string e1 = ex.Message;
}

It is only in the second catch that I receive an error.
But just in case of connection problems I want to try it (the
executereader) again after a few seconds.

I receive an SQLException : but how can I see that it is an connection
problem?
And how can I test this when I change my connection to an oracle
database without changing my code in the catch block.

I just need to know there is an connectionproblem independant of which
database (oracle or SQL) I used so I can take actions.

Thanks in advance,
jac
 
Hi,

If you can get at the IDbConnection that the factory creates then just call
the Open method and catch DbException.

To be perfectly honest, though, it shouldn't matter. If after the code
tests the connection it will immediately use it then you shouldn't need to
test it first. It's possible, due to the asynchronous nature of
inter-process communication, that your Open test will succeed but the
factory's try at it will fail immediately afterwards, meaning that you'll
have to catch DbException there anyway and your code still might not be able
to determine the exact cause of the failure.

Your best bet, IMO, is to just use a try..catch(DbException) around the call
to ExecuteReader and forget about determining the exact cause of the
failure. Is there any particular reason why you want to do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top