Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ or to a Databricks Runtime interactive cluster (e.g. /sql/protocolv1/o/123456789

## Transaction Support

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.
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.

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

Expand Down
16 changes: 11 additions & 5 deletions TRANSACTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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.

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`.

The behavior and examples in the rest of this guide assume `ignore_transactions=False` on every connection, including `conn1` and `conn2`.

## Autocommit Behavior

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

connection = sql.connect(
server_hostname="your-workspace.cloud.databricks.com",
http_path="/sql/1.0/warehouses/abc123"
http_path="/sql/1.0/warehouses/abc123",
ignore_transactions=False,
)

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

To use explicit transactions, disable autocommit:
To use explicit transactions on a connection opened with `ignore_transactions=False`, disable autocommit:

```python
connection.autocommit = False
Expand Down Expand Up @@ -242,7 +247,8 @@ By default, the `autocommit` property returns a cached value for performance. If
connection = sql.connect(
server_hostname="your-workspace.cloud.databricks.com",
http_path="/sql/1.0/warehouses/abc123",
fetch_autocommit_from_server=True
ignore_transactions=False,
fetch_autocommit_from_server=True,
)

# Each access queries the server
Expand Down Expand Up @@ -367,12 +373,12 @@ finally:

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

```sql
CREATE TABLE my_table (id INT, value STRING)
USING DELTA
TBLPROPERTIES ('delta.feature.catalogOwned-preview' = 'supported')
TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')
```

## Related APIs
Expand Down
36 changes: 19 additions & 17 deletions examples/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
access_token=os.getenv("DATABRICKS_TOKEN"),
ignore_transactions=False,
) as connection:

# Disable autocommit to use explicit transactions
connection.autocommit = False

with connection.cursor() as cursor:
# DDL requires autocommit mode.
cursor.execute(
"CREATE TABLE IF NOT EXISTS accounts (id int, balance int) USING DELTA "
"TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')"
)
cursor.execute(
"CREATE TABLE IF NOT EXISTS transfers (from_id int, to_id int, amount int) "
"USING DELTA TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')"
)

connection.autocommit = False
try:
# Create tables for demonstration
cursor.execute("CREATE TABLE IF NOT EXISTS accounts (id int, balance int)")
cursor.execute(
"CREATE TABLE IF NOT EXISTS transfers (from_id int, to_id int, amount int)"
)
connection.commit()

# Start a new transaction - transfer money between accounts
cursor.execute("INSERT INTO accounts VALUES (1, 1000), (2, 500)")
cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
Expand All @@ -29,13 +31,6 @@
connection.commit()
print("Transaction committed successfully")

# Verify the results
cursor.execute("SELECT * FROM accounts ORDER BY id")
print("Accounts:", cursor.fetchall())

cursor.execute("SELECT * FROM transfers")
print("Transfers:", cursor.fetchall())

except Exception as e:
# Roll back on error - all changes are discarded
connection.rollback()
Expand All @@ -45,3 +40,10 @@
finally:
# Restore autocommit to default state
connection.autocommit = True

# Verify the results
cursor.execute("SELECT * FROM accounts ORDER BY id")
print("Accounts:", cursor.fetchall())

cursor.execute("SELECT * FROM transfers")
print("Transfers:", cursor.fetchall())
Loading