Page 1 of 1

How to poll if the connection is still alive?

Posted: Fri 24 Nov 2006 09:39
by m_dirk
Hello all,

I'm using Psion terminals which are connected to our database via a wireless lan. Sometimes it happens that the wireless signal quality is too low, so the connection gets closed. What I need is a fast way to determine if the connection is still open. At this moment I'm doing it in the following manner:

Code: Select all

    Private Shared Function PgSqlPing() As Boolean
        Dim query As String = "select 0"
        Dim command As New PgSqlCommand
        Try
            command.CommandText = query
            command.Connection = m_connection
            command.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            Return False
        Finally
            command.Dispose()
        End Try
    End Function
This function is called each time before I execute a query and if it returns false I try to open the connection again for a number of times. However this function is too slow for my liking. It takes about 300-500 msecs to check and that is way too much.

Is there any faster way to determine if my connection is still open?

Note 1: I tried to use the PgSqlConnection.StateChangeEvent, but it is only triggered after you try to use the connection. If that weren't the case I would have been using this event to track my connection's state.
Note 2: I saw the MySql Library indeed has a Ping() function. Will it be in the PgSql Library too (or is this not possible)?

Posted: Fri 24 Nov 2006 10:27
by Alexey
The way you use is the best from our point of view.

Posted: Fri 24 Nov 2006 10:35
by m_dirk
Alexey wrote:The way you use is the best from our point of view.
Alexey, was I right about the fact that the PgSqlConnection.StateChange event is only triggered after one tries to use the connection? Since I would really like to use it instead of issuing some sort of ping before each query.

Posted: Fri 24 Nov 2006 11:37
by Alexey
You were right.