Re: How to reconnect to a db automatically?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks Armin
"Armin Zingler" <az.nospam@xxxxxxxxxx> wrote in message
news:eidQ99fVHHA.4028@xxxxxxxxxxxxxxxxxxxxxxx
"Robert Dufour" <bdufour@xxxxxxxxxx> schrieb
My functions or subs that connect to my databases are always using
the following code pattern (Vs2003)

Private Mysub ()
Dim MyConn as new Odbc.connection
MyConn.ConnectionString = g_ODBCConnStr 'a global variable that
was tested at application launch to make sure it was valid and could
open the connection, if not the application would not launch and the
app.config value would need to be changed.
Dim MyComd as new Odbc.Command
MyComd.Connection = MyConn
MyCmd.CommandText = "select whatever from whateverTable"
Try
MyConn.open
If Myconn.State = ConnectionState.open Then
'Do whatever needs to be done

else
'The connection could not be opened

end if


Catch ex as exception



End try
MyConn.close

end sub


Is it really necessary to check myconn.state after opening the connection,
and also catch an exception? I think if it can't be opened you get an
exception and don't need an additional check.


My question is as follows. If my connection could not be opened in
this sub, it means that something unexpected happened, like a server
down, a network error because as stated above, at application start
, the connection was tested and app would not launch were it not
available.
How can I detect precisely what caused the attempt at opening a
connection to fail?
For instance if the sql server is just rebooting after an update,
how could I tell?
I think I would try to differentiate between serious hardware
problems like network down or server stopped and other problems like
time out. In some cases it might be worth trying a number of times
to reconnect in other cases just let it go and have tech support
trouble shoot.

Does anyone have any opnions on this? My app is one that should run
24- 7 -365 with no user interaction. It sits on a server in a
backroom, but it is business critical and so I put some code in to
advise me in case of exceptions and important errors, but if the
connection is out temporarely only I don't want to have to spend
time trouble shooting it.

Any insights would be greatly appreciated.
Bob


The documentation on the Open method lists the possible Exceptions. One is
the OdbcException. It has an Errors property - a collection - and each
item
in the collection (class OdbcError) has a nativeError property (and
others).
You can evaluate this property to get detailled information and perform
the
required actions.


Armin



.



Relevant Pages

  • Re: Launch a process for each COM object?
    ... "Once an application is connected to a class object with CoGetClassObject, ... COM server instance. ... If COM can't be configured to launch separate processes, ... consider a COM connection to a single process which then iteself launces ...
    (microsoft.public.win32.programmer.ole)
  • Re: IIS ASP.net Web Services Disconnected
    ... does show no exception, and it is a perfect run on the server-side. ... the connection idle for more than 1/2 hours, ... the server thinks the file is send without problem. ... At the client-side, it times out after 1.5 hours if nothing happen. ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: How to reconnect to a db automatically?
    ... Dim MyConn as new Odbc.connection ... 'The connection could not be opened ... Catch ex as exception ... For instance if the sql server is just rebooting after an update, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: HttpWebRequest.GetRequestStream:Unable to connect to the remot
    ... System.Net.WebException Unable to connect to the remote server at ... Inner exception: System.Net.Sockets.SocketException Message: A connection ... executed on the web server when invoking a remote web service. ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: System.Security.SecurityException {Beginner working with ASP.NET}
    ... seems like the server is runnning with partial trust. ... In the page_load I fill the data adapter, ... grid and close the connection. ... I get this exception when attempting to view my webform in IE6. ...
    (microsoft.public.dotnet.framework.aspnet.security)