Skip to content

Commit 29cd8f8

Browse files
committed
feat: Add multi-statement transaction support
Implement PEP 249-compliant transaction control with extensions for manual commit/rollback operations. This enables atomic multi-table operations with REPEATABLE_READ isolation semantics. Core API additions: - connection.autocommit property for enabling/disabling auto-commit mode - connection.commit() to commit active transactions - connection.rollback() to rollback active transactions - connection.get_transaction_isolation() returns current isolation level - connection.set_transaction_isolation() validates isolation level - TransactionError exception for transaction-specific failures Implementation details: - Added autocommit state caching in Session with optional server query - Added TRANSACTION_ISOLATION_LEVEL_REPEATABLE_READ constant - All transaction operations include proper error handling and telemetry - Supports fetch_autocommit_from_server connection parameter Testing: - Unit tests covering all transaction methods and error scenarios - e2e integration tests validating transaction behavior including multi-table atomicity, sequential transactions, and isolation semantics Documentation: - Comprehensive TRANSACTIONS.md guide with examples and best practices - Updated README.md with basic usage and reference to detailed docs Requires MST-enabled Databricks SQL warehouse and Delta tables with 'delta.feature.catalogOwned-preview' table property.
1 parent fd65fd2 commit 29cd8f8

File tree

8 files changed

+1674
-20
lines changed

8 files changed

+1674
-20
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ or to a Databricks Runtime interactive cluster (e.g. /sql/protocolv1/o/123456789
6767
> to authenticate the target Databricks user account and needs to open the browser for authentication. So it
6868
> can only run on the user's machine.
6969
70+
## Transaction Support
71+
72+
The connector supports multi-statement transactions with manual commit/rollback control:
73+
74+
```python
75+
import os
76+
from databricks import sql
77+
78+
connection = sql.connect(
79+
server_hostname=os.getenv("DATABRICKS_HOST"),
80+
http_path=os.getenv("DATABRICKS_HTTP_PATH")
81+
)
82+
83+
# Disable autocommit to use explicit transactions
84+
connection.autocommit = False
85+
86+
cursor = connection.cursor()
87+
try:
88+
cursor.execute("INSERT INTO table1 VALUES (1, 'a')")
89+
cursor.execute("INSERT INTO table2 VALUES (2, 'b')")
90+
connection.commit() # Commit both inserts atomically
91+
except Exception as e:
92+
connection.rollback() # Rollback on error
93+
raise
94+
95+
cursor.close()
96+
connection.close()
97+
```
98+
99+
For detailed information about transaction behavior, isolation levels, error handling, and best practices, see [TRANSACTIONS.md](TRANSACTIONS.md).
100+
70101
## SQLAlchemy
71102
Starting from `databricks-sql-connector` version 4.0.0 SQLAlchemy support has been extracted to a new library `databricks-sqlalchemy`.
72103

0 commit comments

Comments
 (0)