How to poll if the connection is still alive?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
m_dirk
Posts: 9
Joined: Fri 24 Nov 2006 09:25

How to poll if the connection is still alive?

Post by m_dirk » Fri 24 Nov 2006 09:39

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)?

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 24 Nov 2006 10:27

The way you use is the best from our point of view.

m_dirk
Posts: 9
Joined: Fri 24 Nov 2006 09:25

Post by m_dirk » Fri 24 Nov 2006 10:35

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.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 24 Nov 2006 11:37

You were right.

Post Reply