Skip to content

Conversation

@subrata-ms
Copy link
Contributor

@subrata-ms subrata-ms commented Nov 24, 2025

AB#40573

GitHub Issue: #337


Summary

This pull request addresses a critical issue in connection pooling for the mssql_python library by ensuring that the transaction isolation level is properly reset when a pooled connection is reused. Previously, the isolation level could leak between usages, potentially causing unexpected behavior. The changes include an explicit reset of the isolation level in the connection reset logic and a new test to verify this behavior.

Bug fix for connection pooling session state:

  • Explicitly reset the transaction isolation level to READ COMMITTED in the Connection::reset() method to prevent isolation level settings from leaking between pooled connection usages. This addresses the limitation of SQL_ATTR_RESET_CONNECTION, which does not reset the isolation level. (mssql_python/pybind/connection/connection.cpp)

Testing improvements:

  • Added a new test test_connection_pooling_isolation_level_reset in tests/test_009_pooling.py to verify that the isolation level is reset to the default when a connection is reused from the pool, preventing session state leakage.
  • Imported the mssql_python module in tests/test_009_pooling.py to support new test functionality.

Copilot AI review requested due to automatic review settings November 24, 2025 14:32
@github-actions github-actions bot added the pr-size: small Minimal code update label Nov 24, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a critical bug in connection pooling where the transaction isolation level was not being reset when connections were returned to and reused from the pool. The fix ensures that READ COMMITTED (the SQL Server default) is explicitly set during connection reset, preventing session state leakage between pooled connection usages.

Key changes:

  • Added explicit transaction isolation level reset to READ COMMITTED in the Connection::reset() method after SQL_ATTR_RESET_CONNECTION is called
  • Added comprehensive test to verify that isolation level is properly reset when connections are reused from the pool

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
mssql_python/pybind/connection/connection.cpp Adds explicit reset of transaction isolation level to SQL_TXN_READ_COMMITTED in the Connection::reset() method to prevent isolation level leakage between pooled connection usages
tests/test_009_pooling.py Imports mssql_python module and adds test_connection_pooling_isolation_level_reset test to verify isolation level is reset to default when connections are reused from the pool

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

github-actions bot commented Nov 25, 2025

📊 Code Coverage Report

🔥 Diff Coverage

50%


🎯 Overall Coverage

75%


📈 Total Lines Covered: 5094 out of 6757
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/pybind/connection/connection.cpp (50.0%): Missing lines 319-322

Summary

  • Total: 8 lines
  • Missing: 4 lines
  • Coverage: 50%

mssql_python/pybind/connection/connection.cpp

Lines 315-326

  315     LOG("Resetting transaction isolation level to READ COMMITTED");
  316     ret = SQLSetConnectAttr_ptr(_dbcHandle->get(), SQL_ATTR_TXN_ISOLATION,
  317                                 (SQLPOINTER)SQL_TXN_READ_COMMITTED, SQL_IS_INTEGER);
  318     if (!SQL_SUCCEEDED(ret)) {
! 319         LOG("Failed to reset transaction isolation level (ret=%d). Marking as dead.", ret);
! 320         disconnect();
! 321         return false;
! 322     }
  323     
  324     updateLastUsed();
  325     return true;
  326 }


📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.row.py: 66.2%
mssql_python.pybind.ddbc_bindings.cpp: 67.1%
mssql_python.helpers.py: 67.5%
mssql_python.pybind.connection.connection.cpp: 73.6%
mssql_python.pybind.ddbc_bindings.h: 76.9%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 82.5%
mssql_python.cursor.py: 83.8%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Copy link
Contributor

@saurabh500 saurabh500 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need verification for Txn Level altered with ALTER DATABASE

@subrata-ms subrata-ms requested a review from saurabh500 December 2, 2025 12:26
Copy link
Contributor

@saurabh500 saurabh500 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided some feedback. For its current scope the PR looks good.

@subrata-ms subrata-ms merged commit 5c77e3f into main Dec 3, 2025
27 checks passed
@subrata-ms subrata-ms deleted the subrata-ms/IsolationLevelFix branch December 3, 2025 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: small Minimal code update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants