I have an old visual basic 6 application when some users reported me errors when the computer going back from sleep. This problem did not occurs on every client computer, (I would say some Windows 7). If the vb6 application was still open then if they try to use this application it crashes with the following error message.

I debugged and I found the problem: I have a global variable that keep the connection to the database. This variable is initialized only once in the beginning of the application. When the computer going to sleep and go back some times later, the status of this variable is still “OPEN” but in fact the connection is lost! If I “CLOSE” and then “OPEN” this variable connection I am able to query the database.
I wonder if this is normal that I lost my database connection?!
Here is some code:
' This is my global variable
Global cn As New ADODB.Connection
' Set connection properties for sql server.
cn.ConnectionTimeout = 25
cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = ".\SQL2008"
cn.Properties("Initial Catalog").Value = DB_INITIAL_CATALOG
cn.Properties("User ID").Value = DB_USERNAME
cn.Properties("Password").Value = DB_PASSWORD
cn.Open
' This is a typical query on my database
Set rs = New ADODB.Recordset
strSql = "SELECT * FROM tblUsers"
rs.Open strSql, cn, adOpenKeyset
Any idea?
Thanks.
Yes, I’ve seen this kind of thing before. There may be settings on the connect string that can help reduce it, but time outs and/or network drops can break the underlying (often TCP) connection to the DB server. You then see the error manifest itself in the next I/O to the database.
I recommend wrapping access to the shared connection so you can transparently catch that specific error and retry. Keep the connection private in a class or module and have methods such as:
You could even periodically perform a no-op db operation like quering the catalog or whatever to keep the connection live and proactivle reconnect. You couild also watch for large jumps in time that happen if a computer hibernates.