Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: SQL
Expertise: Beginner
Mar 24, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Checking for a lost connection

I want to periodically check to see if a user's database connection has been lost. How can I?

First of all, a solution. You can implement periodic 'polling' actions by either using the timer OCX on Windows platforms for Version 2, or you can take advantage of the TestCommand() method, which executes several times a second. You can declare a static counter variable and check for the connection every n times. Using the TestCommand() method for any type of complex processing can degrade overall system performance, so use it with care.

The only way to check for an existing database connection is to fire off a SQL statement. You can't simply test the connection with the IsConnected() method of the session, since that will simply check to see if the client application thinks it's connected.

However, I would personally advise against using a polling process for this type of check. Why bother to fire off a SQL statement just to test a connection? An easier way to handle this would be to put code into the DBError() method to handle a lost connection. If there is any error in the automatic database access, it will fire the DBError() method, so you can be sure of catching the problem if it occurs. Besides, why bother checking for a lost connection if the user may not even need to access the database? In a client/server system, it's best to lay back and react to the user's actions.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date