---
title: "Simplifying Resource Management in mssql-python through Context Manager"
description: "The mssql-python driver for SQL Server and Azure SQL now supports Python context managers on both connections and cursors."
url: "https://swe.quest/articles/simplifying-resource-management-in-mssql-python-through-context-manager-e29f76716086532b"
canonical: "https://devblogs.microsoft.com/python/simplifying-resource-management-in-mssql-python-through-context-manager/"
source: "Microsoft for Python Developers"
published: "2025-09-26"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["python","databases"]
---

# Simplifying Resource Management in mssql-python through Context Manager

A post on [Microsoft for Python Developers](https://swe.quest/sources/ms-python.md), published 2025-09-26, 6 min read, How-to. Original: [https://devblogs.microsoft.com/python/simplifying-resource-management-in-mssql-python-through-context-manager/](https://devblogs.microsoft.com/python/simplifying-resource-management-in-mssql-python-through-context-manager/)

## Summary

The mssql-python driver for SQL Server and Azure SQL now supports Python context managers on both connections and cursors. A \`with connect(...)\` block commits on success and rolls back on exception when autocommit is off, and it always closes the connection. A \`with conn.cursor()\` block only guarantees the cursor is closed and leaves transaction control to the connection. Code examples compare this to the equivalent try/finally boilerplate and show nested usage for web requests, ETL moves, reporting queries and atomic fund transfers.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Python](https://swe.quest/tags/python.md) (Languages)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- Python with statement ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- database transactions ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))

## Keywords

mssql-python, SQL Server, Azure SQL, context managers

## Related posts

- [Introducing Apache Arrow Support in mssql-python](https://swe.quest/articles/introducing-apache-arrow-support-in-mssql-python-e10136b97e324bc3.md): Microsoft for Python Developers · 2026-05-04 · Medium · 6 min read · Python, Analytics & OLAP, Performance engineering, Databases
  mssql-python can now fetch SQL Server result sets directly into Apache Arrow buffers, filled by a C++ fetch loop, so no Python object is created per row.
- [Write SQL Your Way: Dual Parameter Style Benefits in mssql-python](https://swe.quest/articles/write-sql-your-way-dual-parameter-style-benefits-in-mssql-python-5967e83bc5a08c68.md): Microsoft for Python Developers · 2026-04-07 · Medium · 7 min read · Python, SQL, Databases, API design
  mssql-python, Microsoft's Python driver for SQL Server and Azure SQL, now accepts both DB-API qmark (?) and pyformat (%(name)s) parameter styles, choosing between them by whether a tuple/list or a dict is passed.
- [ClickHouse and Power BI, columnar end to end: the Arrow Flight connector is in public preview](https://swe.quest/articles/clickhouse-and-power-bi-columnar-end-to-end-the-arrow-flight-connector-is-in-319079df226af9e9.md): ClickHouse · 2026-11-19 · Medium · 7 min read · Analytics & OLAP, Networking, Performance engineering
  ClickHouse's new Power BI connector, now in public preview, talks Arrow Flight SQL over TLS-encrypted gRPC.
- [We’re All Just Looking for Connection](https://swe.quest/articles/we-re-all-just-looking-for-connection-b10700c8e0417e48.md): Slack Engineering · 2024-10-10 · Medium · 7 min read · Python, Networking, Debugging, Concurrency, Incidents & postmortems
  Spikes of EOFError on SQL queries in Quip's Python backend (which also powers Slack canvas) traced back to connection-state checks that only tracked closes initiated by the application.
- [ClickHouse.Driver 1.0.0: The Official .NET Client Hits Stable](https://swe.quest/articles/clickhouse-driver-1-0-0-the-official-net-client-hits-stable-0f7ef38e134a4666.md): ClickHouse · 2026-03-02 · Medium · 5 min read · .NET, Analytics & OLAP, Developer tools
  ClickHouse.Driver 1.0.0 is the first stable release of the official .NET client for ClickHouse, committing to a semver-stable public API.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
