Skip to content

fix: MSSQL connection left in busy state after insert#2171

Merged
abondar merged 4 commits intotortoise:developfrom
seladb:fix-mssql-bulk-create-v2
Apr 7, 2026
Merged

fix: MSSQL connection left in busy state after insert#2171
abondar merged 4 commits intotortoise:developfrom
seladb:fix-mssql-bulk-create-v2

Conversation

@seladb
Copy link
Copy Markdown
Contributor

@seladb seladb commented Apr 6, 2026

Close cursor properly in ODBCTransactionWrapper.execute_many.

Description

We recently see a lot of these errors in MSSQL CI which relies on pyodbc:

[HY000] [Microsoft][ODBC Driver 18 for SQL Server]Connection is busy with results for another command (0) (SQLExecDirectW)

bulk_create routes through execute_many, which doesn't close the cursor properly in ODBCTransactionWrapper.execute_many. This PR tries to fix that.

Motivation and Context

Fix failing CI in MSSQL

How Has This Been Tested?

Run MSSQL in CI and make sure the error doesn't show up.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 6, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing seladb:fix-mssql-bulk-create-v2 (e928788) with develop (a394ab2)

Open in CodSpeed

@seladb seladb marked this pull request as ready for review April 6, 2026 01:37
@seladb
Copy link
Copy Markdown
Contributor Author

seladb commented Apr 6, 2026

@abondar this is the fix you suggested in #2169 and it seems to be working. Much better than my fix, thanks! 🙏

@abondar
Copy link
Copy Markdown
Member

abondar commented Apr 6, 2026

Can you please remove SET NOCOUNT ON; from https://github.com/tortoise/tortoise-orm/blob/develop/tortoise/backends/mssql/client.py#L68 ? I expect it is not needed anymore if we have fixed the root cause

@seladb
Copy link
Copy Markdown
Contributor Author

seladb commented Apr 7, 2026

Can you please remove SET NOCOUNT ON; from https://github.com/tortoise/tortoise-orm/blob/develop/tortoise/backends/mssql/client.py#L68 ? I expect it is not needed anymore if we have fixed the root cause

@abondar this change doesn't work: https://github.com/tortoise/tortoise-orm/actions/runs/24065691084/job/70191195132?pr=2171

According to Claude:

The root cause is that without SET NOCOUNT ON, SQL Server emits a row-count result set for the INSERT before your SELECT @@IDENTITY result. So when you call cursor.fetchone(), pyodbc is sitting on that row-count result (which isn't a query), not on the @@IDENTITY result — hence "Previous SQL was not a query."
SET NOCOUNT ON suppressed that row-count message, which is why the original code worked.

@abondar abondar merged commit 2183f86 into tortoise:develop Apr 7, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants