connection timeout ?

191 views
Skip to first unread message

erikm

unread,
Mar 18, 2009, 4:27:27 PM3/18/09
to pyodbc
Is there anyway to establish the connection timeout with pyodbc?

I have to connect to a database that is remote and sometimes the vpn
between the two machines goes down.

Pyodbc seems to try to connect for about 4 minutes before timing out
with this error:

('08S01', '[08S01] [unixODBC][FreeTDS][SQL Server]Server is
unavailable or does not exist. (20009)')

It would be great to be able to set the timeout so that the script can
continue with displaying an error to the use quicker.

Thanks.

erikm

unread,
Mar 18, 2009, 4:44:31 PM3/18/09
to pyodbc
As a work-around I am wrapping the calls with a call to test_socket
('192.168.10.10','1433'):

def test_socket(host,port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
s.shutdown(2)
return 1
except:
return 0

mkleehammer

unread,
Mar 18, 2009, 11:47:10 PM3/18/09
to pyodbc
At this time, there isn't an established way to do so.

The ODBC way to do so would be to set SQL_ATTR_CONNECTION_TIMEOUT and/
or SQL_ATTR_LOGIN_TIMEOUT to the appropriate value before making the
call.

Here are some potential solutions:

Add another Python-ish keyword (timeout=xxx or connection_timeout=xxx)

Allow some of the connection attributes to be passed as constants:
connect(SQL_ATTR_CONNECTION_TIMEOUT=xxx). This is very open ended and
doesn't require new keywords all the time, but it isn't the most
readable. (Alternatively, we could munge them as long as pyodbc can
recognize them: connect(attr_connection_timeout=xxx). I'm not sure
that's any better, though.)

Provide a specific dictionary that these are passed in: connect(attrs=
{ SQL_ATTR_CONNECTION_TIMEOUT : x}). This looks worse, but does have
the advantage that you can pass in integer constants that pyodbc
doesn't know about, perhaps database-specific ones:

# I'm making this up, of course
ORACLE_ATTR_TIMEOUT = 999
cnxn = connect(cstring, attrs={ ORACLE_ATTR_TIMEOUT : 30 })

Thoughts on these? How great is the need? I'm looking to release
2.1.5 in a day or two.

Erik Myllymaki

unread,
Mar 19, 2009, 2:21:20 PM3/19/09
to pyo...@googlegroups.com
mkleehammer wrote:
> Thoughts on these? How great is the need? I'm looking to release
> 2.1.5 in a day or two.
>
I am making do without so my need isn't dire.

Passing the dictionary in seems like the best of two options above.

Thanks.

Reply all
Reply to author
Forward
0 new messages