smaudet / pypyodbc

Automatically exported from code.google.com/p/pypyodbc
0 stars 0 forks source link

Unable to set SQL Connect timeout #24

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Unable to set SQL Connect timeout

What steps will reproduce the problem?

1. Сonnect to any server that drop (not reject!) the connection

import pypyodbc
conn = 
pypyodbc.Connection("Driver=FreeTDS;Server=192.0.2.1;port=1443;UID=sa;PWD=secret
;database=master;TDS_Version=8.0", timeout=5)

What is the expected output? What do you see instead?

Connection hang longer than 5s because Connection.__init__ automaticaly calls 
self.connect() that sets SQL_ATTR_LOGIN_TIMEOUT but not 
SQL_ATTR_CONNECTION_TIMEOUT

What version of the product are you using? On what operating system?

version = '1.1.5'
Linux

Please provide any additional information below.

I looked through the code and there is settimeout method that is not used. So 
quick solution is simply call self.settimeout(timeout) before self.connect in 
__init__ method.

--- pypyodbc.py.orig    2013-09-06 19:47:05.000000000 +0400
+++ pypyodbc.py 2013-09-06 19:47:27.000000000 +0400
@@ -2285,6 +2285,7 @@
         ret = ODBC_API.SQLAllocHandle(SQL_HANDLE_DBC, shared_env_h, ADDR(self.dbc_h))
         check_success(self, ret)

+        self.settimeout(timeout)
         self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)

But more elegant solution is separate timeouts:
SQL_ATTR_CONNECTION_TIMEOUT
SQL_ATTR_LOGIN_TIMEOUT
SQL_ATTR_QUERY_TIMEOUT

Original issue reported on code.google.com by aleksey....@gmail.com on 6 Sep 2013 at 3:52

GoogleCodeExporter commented 8 years ago

Original comment by jiangwen...@gmail.com on 9 Sep 2013 at 5:01

GoogleCodeExporter commented 8 years ago
In version 1.2.0, self.settimeout(timeout), which sets the connection timeout 
value is also called when setting the login timeout value.

Original comment by jiangwen...@gmail.com on 21 Sep 2013 at 11:12

GoogleCodeExporter commented 8 years ago
In version 1.3.2, the QUERY_TIMEOUT will be set with the value 
connection.timeout (default to 0, which means forever)

CONNECTION_TIMEOUT will default be same as LOGIN_TIMEOUT utill I can think of a 
more elegant way to set them.

Original comment by jiangwen...@gmail.com on 24 May 2014 at 3:24