Skip to content

Connection Context Manager and Commits #635

Description

@benmatwil

Describe the bug

When using a context manager as described in the docs:
https://github.com/microsoft/mssql-python/wiki/Connection#context-manager
there does not seem to be any commit on exit. Are the docs incorrect?

Also do bulkcopies completely ignore the current connection object and create a new one?

To reproduce

import mssql_python

CONN_STR = ...

# Create the table inside a context manager block — no explicit commit
with mssql_python.connect(CONN_STR) as conn:
    conn.execute("CREATE TABLE dbo.no_commit_test (id INT)")
    print("Tried creating table inside context manager")

# Open a new connection and check whether the table exists
with mssql_python.connect(CONN_STR) as conn:
    with conn.cursor() as cursor:
        cursor.tables(table="no_commit_test", schema="dbo", tableType="TABLE")
        exists = cursor.fetchone() is not None

print(f"Table exists after context manager exit: {exists}")
# Expected output: False — the CREATE TABLE was rolled back

Expected behavior

The process described by the docs

Further technical details

Python version: 3.10
SQL Server version: SQL Server 2025
Operating system: Windows/Docker

Metadata

Metadata

Assignees

Labels

area: api-compliancePython API behavior and typing: DB-API 2.0, exceptions, type stubs, new APIs.bugSomething isn't workinginADOtriage doneIssues that are triaged by dev team and are in investigation.under development

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions