[freetds] Detecting dead TCP connection

Andrew Victor avictor.za at gmail.com
Wed Aug 6 14:45:09 EDT 2008


Is there a way to easily /quickly detect if an MS-SQL server has
closed the TCP connection, or the connection is dead?

Our application maintains a pool of open database connections, and
when it gets a request we needs to execute a database stored-procedure
on one of those connections.
We use the ctlib interface, so we call ct_command(), ct_param() and
then ct_send().
But only after the ct_send() step do we get any error: "Write to the
server failed".

Looking at the FreeTDS source, the TCP connection may be marked as
"closed" at the OS level but since FreeTDS hasn't subsequently
performed any operations (read/write/etc) on the socket it does not
know the TCP connection is closed.
It's only when ct_send() tries to send a message, does the OS indicate
the socket has been closed.

Using the TCP KeepAlive mechanism also only informs the OS level
earlier, not FreeTDS or the application.

1. Is there a command that can be used to check the real state
(alive/dead) of the TCP connection?
2. Is there a way that the Client Message Callback could be triggered
earlier?  ie, when the OS detects the connection was closed.
3. Is there a way to auto-reconnect and automatically re-execute the command?

  Andrew Victor

More information about the FreeTDS mailing list