Program crashes when network goes down and back up

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I could definitely use some help with this error! My VB.NET 2003
program only connects to the database when the user saves, retrieves,
etc. If they retrieve a file from the database to work on, and the
network goes down for more than about ten seconds and comes back up,
the next time they try to save the program crashes on the first line
of code that talks to the db. Actually it gets as far as opening the
connection, but breaks on this line: objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection) with this
error:

A first chance exception of type 'System.Data.SqlClient.SqlException'
occurred in system.data.dll
Additional information: System error.

The exception's error message is usually: General network error.
Check your network documentation.

I don't understand! It seems to me that if the network goes down and
comes back up while the program is running but not using the database,
then it should not cause any problems. Any ideas?

Thanks!
Kevin
 
If the network goes down, the underlying connection to the database is
severed and has to be re-established. There are lots of reasons why this is
necessary - for example, if your client is using DHCP you might have an
different IP address once the network is restored.

A good defense against possibly network failure is to put a try/catch
around code that does round-trips to the server, and then if a SQLException
is thrown you can re-open the connection and try again. I think you should
be able to use either the ErrorCode or Number properties of the SQLException
object to distinguish the network error from something else, so you won't
have to parse the message text.

Note that this logic is also good against the possibility that the database
server goes down. In a clustered environment the second server might come up
immediately, but any existing connections to the failed node in the cluster
will have to be reestablished to point to the node that's still running.

- Dave
 
Back
Top