Skip to content

Commit 45ec3bc

Browse files
docs: make transaction examples opt in and runnable
Scope transaction guidance to the explicit opt-in and document compatibility defaults. Create catalog-managed Delta tables in autocommit mode and restore that mode before verifying the transfer results. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>
1 parent 543777a commit 45ec3bc

3 files changed

Lines changed: 31 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ or to a Databricks Runtime interactive cluster (e.g. /sql/protocolv1/o/123456789
103103
104104
## Transaction Support
105105

106-
The connector supports multi-statement transactions with manual commit/rollback control. Set `connection.autocommit = False` to disable autocommit mode, then use `connection.commit()` and `connection.rollback()` to control transactions.
106+
The connector supports multi-statement transactions with manual commit/rollback control. Connect with `ignore_transactions=False` (the default is `True`) to enable transaction APIs. Set `connection.autocommit = False` to disable autocommit mode, then use `connection.commit()` and `connection.rollback()` to control transactions.
107107

108108
For detailed documentation, examples, and best practices, see **[TRANSACTIONS.md](TRANSACTIONS.md)**.
109109

‎TRANSACTIONS.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The Databricks SQL Connector for Python supports multi-statement transactions (MST). This allows you to group multiple SQL statements into atomic units that either succeed completely or fail completely.
44

5+
Enable transaction APIs by passing `ignore_transactions=False` to `sql.connect()`. The default is `True`: assignments to `connection.autocommit` and calls to `connection.commit()` are no-ops, and `connection.rollback()` raises `NotSupportedError`.
6+
7+
The behavior and examples in the rest of this guide assume `ignore_transactions=False` on every connection, including `conn1` and `conn2`.
8+
59
## Autocommit Behavior
610

711
By default, every SQL statement executes in its own transaction and commits immediately (autocommit mode). This is the standard behavior for most database connectors.
@@ -11,7 +15,8 @@ from databricks import sql
1115

1216
connection = sql.connect(
1317
server_hostname="your-workspace.cloud.databricks.com",
14-
http_path="/sql/1.0/warehouses/abc123"
18+
http_path="/sql/1.0/warehouses/abc123",
19+
ignore_transactions=False,
1520
)
1621

1722
# Default: autocommit is True
@@ -23,7 +28,7 @@ cursor.execute("INSERT INTO my_table VALUES (1, 'data')")
2328
# Already committed - data is visible to other connections
2429
```
2530

26-
To use explicit transactions, disable autocommit:
31+
To use explicit transactions on a connection opened with `ignore_transactions=False`, disable autocommit:
2732

2833
```python
2934
connection.autocommit = False
@@ -242,7 +247,8 @@ By default, the `autocommit` property returns a cached value for performance. If
242247
connection = sql.connect(
243248
server_hostname="your-workspace.cloud.databricks.com",
244249
http_path="/sql/1.0/warehouses/abc123",
245-
fetch_autocommit_from_server=True
250+
ignore_transactions=False,
251+
fetch_autocommit_from_server=True,
246252
)
247253

248254
# Each access queries the server
@@ -367,12 +373,12 @@ finally:
367373

368374
To use transactions, you need:
369375
- A Databricks SQL warehouse that supports Multi-Statement Transactions (MST)
370-
- Tables created with the `delta.feature.catalogOwned-preview` table property:
376+
- Tables created with the `delta.feature.catalogManaged` table property:
371377

372378
```sql
373379
CREATE TABLE my_table (id INT, value STRING)
374380
USING DELTA
375-
TBLPROPERTIES ('delta.feature.catalogOwned-preview' = 'supported')
381+
TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')
376382
```
377383

378384
## Related APIs

‎examples/transactions.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
66
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
77
access_token=os.getenv("DATABRICKS_TOKEN"),
8+
ignore_transactions=False,
89
) as connection:
910

10-
# Disable autocommit to use explicit transactions
11-
connection.autocommit = False
12-
1311
with connection.cursor() as cursor:
12+
# DDL requires autocommit mode.
13+
cursor.execute(
14+
"CREATE TABLE IF NOT EXISTS accounts (id int, balance int) USING DELTA "
15+
"TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')"
16+
)
17+
cursor.execute(
18+
"CREATE TABLE IF NOT EXISTS transfers (from_id int, to_id int, amount int) "
19+
"USING DELTA TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')"
20+
)
21+
22+
connection.autocommit = False
1423
try:
15-
# Create tables for demonstration
16-
cursor.execute("CREATE TABLE IF NOT EXISTS accounts (id int, balance int)")
17-
cursor.execute(
18-
"CREATE TABLE IF NOT EXISTS transfers (from_id int, to_id int, amount int)"
19-
)
20-
connection.commit()
21-
2224
# Start a new transaction - transfer money between accounts
2325
cursor.execute("INSERT INTO accounts VALUES (1, 1000), (2, 500)")
2426
cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
@@ -29,13 +31,6 @@
2931
connection.commit()
3032
print("Transaction committed successfully")
3133

32-
# Verify the results
33-
cursor.execute("SELECT * FROM accounts ORDER BY id")
34-
print("Accounts:", cursor.fetchall())
35-
36-
cursor.execute("SELECT * FROM transfers")
37-
print("Transfers:", cursor.fetchall())
38-
3934
except Exception as e:
4035
# Roll back on error - all changes are discarded
4136
connection.rollback()
@@ -45,3 +40,10 @@
4540
finally:
4641
# Restore autocommit to default state
4742
connection.autocommit = True
43+
44+
# Verify the results
45+
cursor.execute("SELECT * FROM accounts ORDER BY id")
46+
print("Accounts:", cursor.fetchall())
47+
48+
cursor.execute("SELECT * FROM transfers")
49+
print("Transfers:", cursor.fetchall())

0 commit comments

Comments
 (0)