title: Connecting to Azure SQL Server with AAD authentication using PyODBC
body: <p><strong>Environment:</strong><br><br>
  <strong>Docker image</strong>: python:2.7.14-stretch (which means Debian 9 as underlying OS)<br>
  Installed SQL Server ODBC driver for Debian 9 (<a href="https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017</a>)<br><br>
  using pyodbc module<br><br>
  Connecting to Azure SQL Server with AAD authentication.<br></p>
  
  <p><strong>Issue:</strong></p>
  
  <p>First connection to DB is made perfectly fine. However any next connection to same DB or other DB is failed with below error:</p>
  
  <blockquote>
    <p>Exception::: ('08001', u'[08001] [unixODBC][Microsoft][ODBC Driver 17
    for SQL Server]SSL Provider:
    [error:140A90F1:lib(20):func(169):reason(241)] (-1)
    (SQLDriverConnect)')
        Cannot connect to DB</p>
  </blockquote>
  
  <p>Please note that this is issue with only when using AAD autnethcation mode. If I use simple sql server user, everything runs fine.</p>
  
  <p>Below is the example code snippet.</p>
  
  <pre><code>db_list = [TEST_DB1, TEST_DB2]
  sql_conn = None
  for db in db_list:
      try:
          conn_string = 'DRIVER={ODBC Driver 17 for SQL Server};' \
                        'SERVER=' + &lt;db_url&gt; + \
                        ';DATABASE=' + &lt;db_name&gt; + \
                        ';UID=' + &lt;db_username&gt; + \
                        ';PWD=' + &lt;db_password&gt; + \
                        ';Authentication=ActiveDirectoryPassword'
          print conn_string
          sql_conn = pyodbc.connect(conn_string)
  except Exception as e:
      print "Exception:::", e
      print 'Cannot connect to DB' + str(sys.exc_info()[0])
      return None
  sql_conn.cursor().execute(&lt;some SQL Query&gt;)
  sql_conn.close()
  </code></pre>