@@ -1519,6 +1519,7 @@ class MSSQLConnectionConfig(ConnectionConfig):
15191519 database : t .Optional [str ] = ""
15201520 timeout : t .Optional [int ] = 0
15211521 login_timeout : t .Optional [int ] = 60
1522+ login_attempts : t .Optional [int ] = 1
15221523 charset : t .Optional [str ] = "UTF-8"
15231524 appname : t .Optional [str ] = None
15241525 port : t .Optional [int ] = 1433
@@ -1610,6 +1611,7 @@ def _connection_kwargs_keys(self) -> t.Set[str]:
16101611 "trust_server_certificate" ,
16111612 "encrypt" ,
16121613 "odbc_properties" ,
1614+ "login_attempts" ,
16131615 }
16141616 )
16151617 # Remove pymssql-specific parameters
@@ -1648,8 +1650,9 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16481650 authentication = kwargs .pop ("authentication" , None )
16491651 trust_server_certificate = kwargs .pop ("trust_server_certificate" , False )
16501652 encrypt = kwargs .pop ("encrypt" , True )
1653+ timeout = kwargs .pop ("timeout" , 0 )
16511654 login_timeout = kwargs .pop ("login_timeout" , 59 )
1652- login_attempts = kwargs .pop ("login_attempts" , 1 ) # TODO: document
1655+ login_attempts = kwargs .pop ("login_attempts" , 1 )
16531656
16541657 # Build connection string
16551658 conn_str_parts = [
@@ -1664,6 +1667,11 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16641667 if trust_server_certificate :
16651668 conn_str_parts .append ("TrustServerCertificate=yes" )
16661669
1670+ # `Connection Timeout=` is not a valid option so we leverage `ConnectRetry*`.
1671+ # See the following:
1672+ # - https://github.com/microsoft/mssql-python/issues/339
1673+ # - https://github.com/microsoft/mssql-python/wiki/Connection-to-SQL-Database
1674+ # - https://github.com/microsoft/mssql-python/wiki/Connection#timeout
16671675 conn_str_parts .append (f"ConnectRetryCount={ login_attempts } " )
16681676 conn_str_parts .append (f"ConnectRetryInterval={ min (int (login_timeout ), 60 )} " )
16691677
@@ -1687,6 +1695,8 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
16871695 "pwd" ,
16881696 "encrypt" ,
16891697 "trustservercertificate" ,
1698+ "connectretrycount" ,
1699+ "connectretryinterval" ,
16901700 "connection timeout" ,
16911701 ):
16921702 continue
@@ -1700,7 +1710,11 @@ def connect_mssql_python(**kwargs: t.Any) -> t.Callable:
17001710 # Create the connection
17011711 conn_str = ";" .join (conn_str_parts )
17021712
1703- conn = mssql_python .connect (conn_str , autocommit = kwargs .get ("autocommit" , False ))
1713+ conn = mssql_python .connect (
1714+ conn_str ,
1715+ autocommit = kwargs .get ("autocommit" , False ),
1716+ timeout = timeout ,
1717+ )
17041718
17051719 # TODO: Remove this output converter as DATETIMEOFFSET
17061720 # should be handled natively by `mssql-python`.
0 commit comments