Skip to content

[QUESTION] Are multiple active result sets supported? #395

Description

Are multiple active result sets supported, i.e. can I have multiple active cursors on the same connection? In pyodbc, for example, this is supported via the connection property MultipleActiveResultSets=True.

Activity

  1. added
    question
    on Jan 7, 2026
  2. bewithgaurav commented on Jan 7, 2026

    @bewithgaurav
    Collaborator

    Hi Martin Studer (@martinstuder)
    We don't support MultipleActiveResultSets as of now.

  3. martinstuder commented on Jan 12, 2026

    @martinstuder
    Author

    Thanks for the feedback Gaurav Sharma (@bewithgaurav). Do you see this as something that will eventually be supported and would it therefore make sense to open a feature request for it?

    I guess the current workaround is to use separate connections: https://github.com/microsoft/mssql-python/wiki/Database-Transaction-Management#concurrent-database-transactions

  4. David-Engel commented on Jan 14, 2026

    @David-Engel
    Contributor

    Do you see this as something that will eventually be supported and would it therefore make sense to open a feature request for it?

    You already have opened a feature request. This issue. 😃

    That said, what's your use case / the usage pattern you have specifically in Python that requires MARS? We know how it can be used. It just helps to hear the real-world scenarios. Your answer, and upvotes on this issue, will help us prioritize when to bring the feature into mssql-python.

  5. martinstuder commented on Jan 19, 2026

    @martinstuder
    Author

    Our use case is that we typically create a single connection (using DefaultAzureCredential as described at https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-python-quickstart?view=azuresql&tabs=windows%2Csql-inter#add-code-to-connect-to-azure-sql-database) and cache that connection for as long as possible . If connection pooling in mssql_python works with a constantly changing access token we could probably switch to an approach where we always create a new connection.

  6. David-Engel commented on Jan 20, 2026

    @David-Engel
    Contributor

    Our use case is that we typically create a single connection (using DefaultAzureCredential as described at https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-python-quickstart?view=azuresql&tabs=windows%2Csql-inter#add-code-to-connect-to-azure-sql-database) and cache that connection for as long as possible . If connection pooling in mssql_python works with a constantly changing access token we could probably switch to an approach where we always create a new connection.

    Martin Studer (@martinstuder) What you've described doesn't require MARS. MARS is all about interleaving the fetching of results of multiple queries on a single connection. For example, in a parent/child relationship you might query parent records and as you are reading through the parent result set, you might query child records for each parent. (Note: If testing the effect of MARS, ensure parent result is relatively large (> packet size) to ensure the driver isn't just getting all the results in a single response from the server.)

    You could also be using MARS to cover poor practices like leaving results/cursors open before moving on to the next query against the connection. This isn't really a valid use case. You would eventually run into too many open cursors. But it would "work" for relatively short-lived scripts.

  7. martinstuder commented on Jan 26, 2026

    @martinstuder
    Author

    David Engel (@David-Engel) As a result of sharing a single connection we do end up in situations where we interleave the fetching of results of multiple queries. But I think we could adapt our connection management in a way that MARS would no longer be needed. So maybe there is a certain element of "poor practices" that you were mentioning ...

  8. amachanic commented on Jan 29, 2026

    @amachanic

    Martin Studer (@martinstuder) Are you sure pyodbc allows interleaved results? I'm unable to get it working on this end. Maybe I'm doing something wrong?

    conn = pyodbc.connect("Driver={ODBC Driver 18 for SQL Server};Server=myserver;Database=mydb;Uid=myuid;Pwd=mypwd;Encrypt=No;MultipleActiveResultSets=True")
    
    cur = conn.cursor()
    
    x = cur.execute('select * from generate_series(0, 1000000)')
    print(x.fetchone())
    # (0,)
    
    y = cur.execute('select * from generate_series(1000001, 9999999)')
    print(y.fetchone())
    # (1000001,)
    print(x.fetchone())
    # (1000002,)
    
  9. David-Engel commented on Jan 30, 2026

    @David-Engel
    Contributor

    Adam Machanic (@amachanic) Two problems. (1) You need to create a new cursor. (2) The ODBC option to enable MARS is MARS_Connection=Yes.

    cur = conn.cursor()
    x = cur.execute('...') # x is cur
    y = cur.execute('...') # y is the SAME cur, which cancels x's result

  10. amachanic commented on Jan 30, 2026

    @amachanic

    David Engel (@David-Engel) Thanks, that did the trick. I'm extremely surprised to learn that pyodbc supports that. Always seemed like a useless feature to me 🤷

  11. added
    area: api-compliance
    on Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: api-complianceenhancementquestion

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions