denisenkom / pytds

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation
MIT License
191 stars 53 forks source link

Issue connecting to partner database when the database is in mirrored state. #167

Open arpitvarma111 opened 2 months ago

arpitvarma111 commented 2 months ago

The library with version (1.15.0) fails to connect to the partner database when the principal db fails (MS SQL Server). We get the following stack trace exception:

Traceback (most recent call last):
  File "/opt/python/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
  File "/opt/python/sqlalchemy/engine/base.py", line 3288, in raw_connection
    return self.pool.connect()
  File "/opt/python/sqlalchemy/pool/base.py", line 452, in connect
    return _ConnectionFairy._checkout(self)
  File "/opt/python/sqlalchemy/pool/base.py", line 1267, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/opt/python/sqlalchemy/pool/base.py", line 716, in checkout
    rec = pool._do_get()
  File "/opt/python/sqlalchemy/pool/impl.py", line 170, in _do_get
    self._dec_overflow()
  File "/opt/python/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/opt/python/sqlalchemy/pool/impl.py", line 167, in _do_get
    return self._create_connection()
  File "/opt/python/sqlalchemy/pool/base.py", line 393, in _create_connection
    return _ConnectionRecord(self)
  File "/opt/python/sqlalchemy/pool/base.py", line 678, in __init__
    self.__connect()
  File "/opt/python/sqlalchemy/pool/base.py", line 903, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/opt/python/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/opt/python/sqlalchemy/pool/base.py", line 898, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
  File "/opt/python/sqlalchemy/engine/create.py", line 637, in connect
    return dialect.connect(*cargs, **cparams)
  File "/opt/python/sqlalchemy/engine/default.py", line 615, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
  File "/opt/python/pytds/__init__.py", line 398, in connect
    return utils.exponential_backoff(
  File "/opt/python/pytds/utils.py", line 51, in exponential_backoff
    ex_handler(ex)
  File "/opt/python/pytds/__init__.py", line 394, in ex_handler
    raise ex
  File "/opt/python/pytds/utils.py", line 41, in exponential_backoff
    return work(try_time)
  File "/opt/python/pytds/__init__.py", line 357, in attempt
    return _connect(
  File "/opt/python/pytds/__init__.py", line 453, in _connect
    route = tds_socket.login()
  File "/opt/python/pytds/tds_socket.py", line 95, in login
    if not self._main_session.process_login_tokens():
  File "/opt/python/pytds/tds_session.py", line 1571, in process_login_tokens
    self.process_token(marker)
  File "/opt/python/pytds/tds_session.py", line 1586, in process_token
    return handler(self)
  File "/opt/python/pytds/tds_session.py", line 1745, in <lambda>
    tds_base.TDS_DONE_TOKEN: lambda self: self.process_end(tds_base.TDS_DONE_TOKEN),
  File "/opt/python/pytds/tds_session.py", line 430, in process_end
    self.raise_db_exception()
  File "/opt/python/pytds/tds_session.py", line 176, in raise_db_exception
    raise ex
pytds.tds_base.OperationalError: ('Cannot open database "test" requested by the login. The login failed. Login failed for user \'xxx\'.', None)
denisenkom commented 2 months ago

Failover connection is now fixed. But in your case connection is failing with login error which would not be retried, as that could cause lockout. Therefore you need to fix credentials in your connection.