diff --git a/README.md b/README.md index 36162e69b..415c34581 100644 --- a/README.md +++ b/README.md @@ -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)**. diff --git a/TRANSACTIONS.md b/TRANSACTIONS.md index 590c298c0..9ccef4a94 100644 --- a/TRANSACTIONS.md +++ b/TRANSACTIONS.md @@ -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. @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/examples/transactions.py b/examples/transactions.py index 6f58dbd2d..16b2ca72d 100644 --- a/examples/transactions.py +++ b/examples/transactions.py @@ -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") @@ -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() @@ -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())