Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Microsoft for Python Developers 6 min read How-to mediumAdept

Simplifying Resource Management in mssql-python through Context Manager

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.

Related posts

  • Introducing Apache Arrow Support in mssql-python

    Microsoft for Python Developers mediumAdept

    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. Polars, Pandas (ArrowDtype) and DuckDB can then consume the data zero-copy through the Arrow C Data Interface. Three new cursor methods cover the main patterns: arrow_batch returns one RecordBatch, arrow returns a full Table (watch peak memory), and arrow_reader streams batches lazily, with batch_size trading throughput against memory. Gains are largest for temporal types like DATETIMEOFFSET, but NVARCHAR is currently slower on Linux because of the UTF-16 to UTF-8 conversion path, and a fix is planned.

  • Write SQL Your Way: Dual Parameter Style Benefits in mssql-python

    Microsoft for Python Developers mediumAdept

    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. For dicts, the driver scans the SQL for named placeholders (skipping string literals, comments and bracketed identifiers), validates keys, builds a positional tuple that duplicates reused values, and rewrites the query to ? before ODBC binding, so there is no change in database communication or SQL-injection safety. Named parameters help with self-documenting queries, value reuse, dynamic filter building and sharing one dict across queries. Extra dict keys are ignored, mixing styles in one query fails, %% escapes literal patterns, and module paramstyle reports "pyformat".

  • ClickHouse and Power BI, columnar end to end: the Arrow Flight connector is in public preview

    ClickHouse mediumAdept

    ClickHouse's new Power BI connector, now in public preview, talks Arrow Flight SQL over TLS-encrypted gRPC. Data stays columnar end to end instead of being converted to rows and back as the ODBC path does, and the Power BI service connects directly, so no on-premises gateway or driver install is needed. In a 10M-row, 20-column import benchmark, refresh took 278 s versus 390 s for ODBC plus a gateway. ClickHouse spent about 5 s on the query instead of 150 s and used roughly 6x less CPU. The connector supports query folding and exact Decimal/UInt64 types, works best in Import mode (DirectQuery is still maturing), and needs ClickHouse 26.5+ or a support request on ClickHouse Cloud.

  • We’re All Just Looking for Connection

    Slack Engineering mediumAdept

    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. AWS's database proxy began closing connections after 24 hours, and because asyncio's StreamWriter.is_closing() doesn't reflect a peer-initiated FIN (a socket in CLOSE_WAIT can still be read), the code never reconnected before issuing queries. Instrumenting the drift between the connected flag and the actual closed state surfaced six more bugs, including websocket false negatives, stale HTTP pool clients and missed Redis cleanup. Fixing them nearly eliminated the errors and unblocked a stalled migration from a custom runtime to asyncio, where an overloaded RPC peer closing pooled connections had caused hanging PDF exports.

  • ClickHouse.Driver 1.0.0: The Official .NET Client Hits Stable

    ClickHouse mediumAdept

    ClickHouse.Driver 1.0.0 is the first stable release of the official .NET client for ClickHouse, committing to a semver-stable public API. It introduces a thread-safe, singleton-friendly ClickHouseClient that replaces most ADO.NET usage and the deprecated ClickHouseBulkCopy, and adds parameter type extraction from SQL, JWT/roles/custom headers, POCO serialization to JSON columns, mid-stream exception detection and QBit vector support. Breaking changes matter for upgrades: .NET Framework/Standard support is dropped, timezone-less DateTime columns now return Kind=Unspecified, and JSON writes default to string mode parsed server-side, which can change inferred types (e.g. Int32 to Int64).

The quest for software engineering knowledge.

Look:
Layout: