---
title: Transaction modes
description: "Transactions on Databricks support two modes: non-interactive (`BEGIN ATOMIC`) for automatic commit/rollback in jobs, and interactive (`BEGIN TRANSACTION`) for manual control in SQL editors and JDBC clients."
canonical_url: https://docs.databricks.com/aws/en/transactions/transaction-modes
---

# Transaction modes

Transactions on Databricks support two modes:

* **Non-interactive** (`BEGIN ATOMIC`): Automatically commits or rolls back. Best for scheduled jobs and fixed statement sequences.
* **Interactive** (`BEGIN TRANSACTION`): Gives you manual control over commit and rollback. Best for validation, debugging, and JDBC clients.

For requirements and an overview of transactions, see [Transactions](https://docs.databricks.com/aws/en/transactions/index.md). For hands-on practice with both modes, see [Tutorial: Coordinate transactions across tables](https://docs.databricks.com/aws/en/transactions/tutorial.md).

> [!NOTE]
>
> All tables written to in a multi-statement, multi-table transaction must:
>
> * Be Unity Catalog managed tables (Delta or Iceberg)
> * Have [Catalog commits](https://docs.databricks.com/aws/en/tables/features/catalog-commits.md) enabled

## <a id="non-interactive" />Non-interactive transactions

Non-interactive transactions use [SQL scripting](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-scripting.md) with the `ATOMIC` keyword. The [ATOMIC compound statement](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic.md) block runs all statements as a single atomic unit. All succeed together or all fail together.

**Supported compute**: Any [SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/index.md), [serverless compute](https://docs.databricks.com/aws/en/compute/serverless/index.md), or [cluster](https://docs.databricks.com/aws/en/compute/index.md) running Databricks Runtime 18.0 and above.

**Supported syntax**: Supports SQL, Scala `spark.sql` blocks, and PySpark `spark.sql` blocks.

> [!NOTE]
>
> You can use non-interactive transactions within Structured Streaming's `forEachBatch` by calling `spark.sql("BEGIN ATOMIC ... END;")`. However, Structured Streaming checkpoints don't advance transactionally.

### <a id="ni-syntax" />Syntax

```sql
BEGIN ATOMIC
  statement1;
  statement2;
  statement3;
END;
```

Databricks automatically commits all changes if all statements succeed. If any statement fails, Databricks automatically rolls back all changes.

### <a id="ni-sql-editor" />Use in SQL editor

Run non-interactive transactions directly in the [SQL Editor](https://docs.databricks.com/aws/en/sql/user/queries/index.md). Select the entire [ATOMIC compound statement](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic.md) block and run it as a single statement:

```sql
BEGIN ATOMIC
  DELETE FROM staging_sales WHERE load_date < current_date() - INTERVAL 7 DAYS;

  INSERT INTO staging_sales
  SELECT * FROM raw_sales WHERE load_date = current_date();

  MERGE INTO sales AS target
  USING staging_sales AS source
  ON target.sale_id = source.sale_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;
END;
```

### <a id="ni-notebooks" />Use in notebooks

Run non-interactive transactions in notebooks using SQL cells or programmatic APIs.

#### SQL

```sql
BEGIN ATOMIC
  UPDATE inventory SET quantity = quantity - 10 WHERE product_id = 2001;
  UPDATE inventory SET quantity = quantity + 10 WHERE product_id = 2002;
  INSERT INTO inventory_moves (from_product, to_product, quantity, move_date)
  VALUES (2001, 2002, 10, current_date());
END;
```

#### Python

```python
spark.sql("""
BEGIN ATOMIC
  UPDATE inventory SET quantity = quantity - 10 WHERE product_id = 2001;
  UPDATE inventory SET quantity = quantity + 10 WHERE product_id = 2002;
  INSERT INTO inventory_moves (from_product, to_product, quantity, move_date)
  VALUES (2001, 2002, 10, current_date());
END;
""")
```

#### Scala

```scala
spark.sql("""
BEGIN ATOMIC
  UPDATE inventory SET quantity = quantity - 10 WHERE product_id = 2001;
  UPDATE inventory SET quantity = quantity + 10 WHERE product_id = 2002;
  INSERT INTO inventory_moves (from_product, to_product, quantity, move_date)
  VALUES (2001, 2002, 10, current_date());
END;
""")
```

### <a id="ni-jobs" />Use in scheduled jobs

Non-interactive transactions work well in scheduled jobs because they automatically handle commit and rollback:

```sql
BEGIN ATOMIC
  -- Clear previous staging data
  DELETE FROM staging_daily_sales WHERE load_date = current_date();

  -- Load new data
  INSERT INTO staging_daily_sales
  SELECT sale_id, customer_id, amount, sale_date, current_date() as load_date
  FROM raw_sales
  WHERE sale_date = current_date() - INTERVAL 1 DAY;

  -- Validate row count (fails transaction if no data)
  IF (SELECT COUNT(*) FROM staging_daily_sales WHERE load_date = current_date()) = 0 THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'No sales data loaded for yesterday';
  END IF;

  -- Merge into production
  MERGE INTO daily_sales AS target
  USING staging_daily_sales AS source
  ON target.sale_id = source.sale_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;
END;
```

If any statement fails, including the assertion, the entire transaction rolls back automatically.

### <a id="ni-jdbc" />Use with JDBC

External clients can run non-interactive transactions.

#### JDBC

```java
String sql = """
    BEGIN ATOMIC
      INSERT INTO orders (order_id, total) VALUES (1001, 500.00);
      UPDATE customers SET last_order = CURRENT_DATE() WHERE customer_id = 5001;
    END;
    """;

Statement stmt = conn.createStatement();
stmt.execute(sql);
```

### <a id="ni-api" />Use with Statement Execution API

Run non-interactive transactions using the [Statement Execution API](https://docs.databricks.com/api/statement-execution/v1/statement-execution):

```python
import requests

sql = """
BEGIN ATOMIC
  INSERT INTO sales (sale_id, amount) VALUES (3001, 750.00);
  UPDATE daily_totals SET total = total + 750.00 WHERE sale_date = CURRENT_DATE();
END;
"""

response = requests.post(
    f"{workspace_url}/api/2.0/sql/statements",
    headers={"Authorization": f"Bearer {token}"},
    json={
        "warehouse_id": warehouse_id,
        "statement": sql,
        "wait_timeout": "30s"
    }
)
```

### <a id="ni-patterns" />ETL patterns

The following patterns demonstrate common ETL workflows using non-interactive transactions.

#### Staging and validation pattern

This pattern loads data into a staging area, validates data quality, and merges validated records into production tables:

```sql
BEGIN ATOMIC
  -- Load into staging
  INSERT INTO staging_customers
  SELECT * FROM external_source
  WHERE ingest_date = current_date();

  -- Validate data quality
  IF (SELECT COUNT(*) FROM staging_customers WHERE email NOT LIKE '%@%') > 0 THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid email addresses found';
  END IF;

  -- Merge validated data
  MERGE INTO customers AS target
  USING staging_customers AS source
  ON target.customer_id = source.customer_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;

  -- Update metadata
  UPDATE etl_metadata
  SET last_load_date = current_date(),
      rows_processed = (SELECT COUNT(*) FROM staging_customers)
  WHERE table_name = 'customers';
END;
```

#### Dimension and fact table pattern

This pattern updates dimension tables before loading fact tables to maintain referential integrity:

```sql
BEGIN ATOMIC
  -- Update dimension tables first
  MERGE INTO dim_products AS target
  USING staging_products AS source
  ON target.product_id = source.product_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;

  MERGE INTO dim_customers AS target
  USING staging_customers AS source
  ON target.customer_id = source.customer_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;

  -- Then load fact table with foreign key references
  INSERT INTO fact_sales
  SELECT s.sale_id, p.product_id, c.customer_id, s.sale_amount, s.sale_date
  FROM staging_sales s
  JOIN dim_products p ON s.product_id = p.product_id
  JOIN dim_customers c ON s.customer_id = c.customer_id;
END;
```

### <a id="ni-errors" />Error handling

When a statement fails within a [`BEGIN ATOMIC ... END;`](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic.md) block, Databricks rolls back all changes and returns an error message.

Debugging tips:

1. Review the error message to identify which statement failed.
2. Test statements individually outside the transaction block.
3. Add validation checks using [`SIGNAL`](https://docs.databricks.com/aws/en/sql/language-manual/control-flow/signal-stmt.md) to fail with custom error messages.
4. Query transaction history for additional context.

## <a id="interactive" />Interactive transactions

Interactive transactions give you explicit control over transaction boundaries. You manually begin a transaction, run statements, and explicitly [commit](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-commit.md) or [roll back](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-rollback.md).

**Supported compute**: [SQL warehouses](https://docs.databricks.com/aws/en/compute/sql-warehouse/index.md) only.

**Supported syntax**: SQL only.

### <a id="int-syntax" />Syntax

To commit:

```sql
BEGIN TRANSACTION;
statement1;
statement2;
COMMIT;
```

To roll back:

```sql
BEGIN TRANSACTION;
statement1;
statement2;
ROLLBACK;
```

### <a id="int-validation" />Validate before committing

Use interactive transactions to validate results before committing:

```sql
BEGIN TRANSACTION;

-- Load staging data
INSERT INTO staging_customers
SELECT * FROM external_customers
WHERE load_date = current_date();

-- Validate and commit or rollback
BEGIN
  DECLARE duplicate_count INT;
  SET duplicate_count = (
    SELECT COUNT(*) FROM (
      SELECT customer_id, COUNT(*) as cnt
      FROM staging_customers
      WHERE load_date = current_date()
      GROUP BY customer_id
      HAVING COUNT(*) > 1
    )
  );

  IF duplicate_count > 0 THEN
    ROLLBACK;
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate customers found in staging data';
  ELSE
    MERGE INTO customers AS target
    USING staging_customers AS source
    ON target.customer_id = source.customer_id
    WHEN MATCHED THEN UPDATE SET *
    WHEN NOT MATCHED THEN INSERT *;
    COMMIT;
  END IF;
END;
```

### <a id="int-rollback" />Explicit rollback

Roll back a transaction when validation fails or business logic requires discarding changes:

```sql
BEGIN TRANSACTION;

UPDATE inventory
SET quantity = quantity - 50
WHERE product_id = 2001;

-- Check if quantity would go negative
BEGIN
  DECLARE new_quantity INT;
  SET new_quantity = (SELECT quantity FROM inventory WHERE product_id = 2001);

  IF new_quantity < 0 THEN
    ROLLBACK;
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insufficient inventory for product 2001';
  ELSE
    COMMIT;
  END IF;
END;
```

### <a id="int-jdbc" />Use with JDBC

The JDBC driver supports running DML statements using `executeUpdate()` within transactions. For a list of supported DML statements, see [Supported operations](https://docs.databricks.com/aws/en/transactions/index.md#supported-operations).

JDBC clients use interactive transactions by disabling auto-commit mode:

```java
Connection conn = DriverManager.getConnection(jdbcUrl, properties);

try {
    conn.setAutoCommit(false);  // Start transaction mode
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("INSERT INTO accounts (account_id, balance) VALUES (1001, 5000)");
    stmt.executeUpdate("UPDATE accounts SET balance = balance - 100 WHERE account_id = 1001");

    conn.commit();  // Commit the transaction

} catch (SQLException e) {
    conn.rollback();  // Roll back on error
    throw e;
} finally {
    conn.close();
}
```

#### <a id="int-jdbc-not-supported" />Unsupported JDBC operations

The following JDBC operations are not supported within interactive transactions:

| Category                             | Not supported                                                                                |
| ------------------------------------ | -------------------------------------------------------------------------------------------- |
| Catalog or schema switching          | `Connection.setCatalog()` and `Connection.setSchema()`                                       |
| Session configuration changes        | `Connection.setClientInfo()` for session-level properties such as `TIMEZONE` and `ANSI_MODE` |
| All DatabaseMetaData (all protocols) | All `DatabaseMetaData.*` methods                                                             |
| PreparedStatement metadata           | `PreparedStatement.getMetaData()`                                                            |
| Stored procedures                    | `CALL procedure_name()`                                                                      |

### <a id="int-odbc" />Use with ODBC

The ODBC driver supports running DML statements using `SQLExecute()` and `SQLExecDirect()` within transactions. For a list of supported DML statements, see [Supported operations](https://docs.databricks.com/aws/en/transactions/index.md#supported-operations).

ODBC clients can use interactive transactions with the Databricks ODBC driver using standard ODBC transaction management functions.

> [!NOTE]
>
> You must turn off `AutoCommit` to use transactions. To turn off `AutoCommit` at runtime, set `UseNativeQuery` to `1`. See [ANSI SQL-92 query support in ODBC](https://docs.databricks.com/aws/en/integrations/odbc/capability.md#odbc-ansi).

#### <a id="int-odbc-not-supported" />Unsupported ODBC operations

The following ODBC operations are not supported within interactive transactions:

| Category                      | Not supported                                                                                                                                                                            |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| All catalog functions         | `SQLTables`, `SQLColumns`, `SQLStatistics`, `SQLSpecialColumns`, `SQLPrimaryKeys`, `SQLForeignKeys`, `SQLTablePrivileges`, `SQLColumnPrivileges`, `SQLProcedures`, `SQLProcedureColumns` |
| Setting connection attributes | Catalog switching, isolation level changes, and access mode changes using `SQLSetConnectAttr()`                                                                                          |
| SQL translation               | `SQLNativeSql`                                                                                                                                                                           |

### <a id="int-python" />Use with the Databricks SQL Connector for Python

The Databricks SQL Connector for Python supports running DML statements using `cursor.execute()` within transactions. For a list of supported DML statements, see [Supported operations](https://docs.databricks.com/aws/en/transactions/index.md#supported-operations).

Python applications can use interactive transactions with the [Databricks SQL Connector for Python](https://docs.databricks.com/aws/en/dev-tools/python-sql-connector.md) by setting `autocommit=False`:

```python
from databricks import sql

with sql.connect(
    server_hostname="dbc-a1b2345c-d6e7.cloud.databricks.com",
    http_path="sql/1.0/warehouses/abc123def456",
    access_token="your-access-token",
    autocommit=False
) as connection:
    with connection.cursor() as cursor:
        cursor.execute("INSERT INTO accounts (account_id, balance) VALUES (1001, 5000)")
        cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE account_id = 1001")
        connection.commit()
```

#### <a id="int-python-not-supported" />Unsupported Python connector operations

The following Python connector operations are not supported within interactive transactions:

| Category     | Not supported                                                                  |
| ------------ | ------------------------------------------------------------------------------ |
| All metadata | `cursor.catalogs()`, `cursor.schemas()`, `cursor.tables()`, `cursor.columns()` |

### <a id="int-driver-limitations" />Driver limitations for interactive transactions

The following limitations apply to all drivers when using interactive transactions.

Metadata operations are not supported inside interactive transactions. The following operations might fail within a transaction regardless of the driver or protocol:

| Driver/Protocol  | Type               | Methods                                                                         |
| ---------------- | ------------------ | ------------------------------------------------------------------------------- |
| JDBC             | `DatabaseMetaData` | `getCatalogs()`, `getSchemas()`, `getTables()`, `getColumns()`, `getTypeInfo()` |
| ODBC             | Catalog functions  | `SQLTables`, `SQLColumns`, `SQLGetTypeInfo`                                     |
| Python connector | Metadata methods   | `cursor.catalogs()`, `cursor.schemas()`, `cursor.tables()`, `cursor.columns()`  |
| SQL              | Metadata commands  | `SHOW TABLES` and `SHOW DATABASES`                                              |

Databricks recommends that you run these metadata operations outside of transactions.

> [!WARNING]
>
> Running transactions on multiple threads on a single driver connection object leads to undefined behavior. Run only one transaction at a time on each connection object.

### <a id="int-isolation" />Isolation behavior

Uncommitted changes in an interactive transaction are only visible to your session. Changes are only visible to other sessions after you run `COMMIT;`.

> [!NOTE]
>
> Interactive transactions use more conservative conflict detection than non-interactive transactions and can conflict at the table level (except for unconditional appends). For row-level conflict detection, use non-interactive transactions ([`BEGIN ATOMIC ... END;`](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic.md)).

1. To verify isolation, create the sample table if it doesn't exist:

```sql
CREATE TABLE IF NOT EXISTS sample_accounts (
  id INT,
  account_name STRING,
  balance DECIMAL(10,2)
) USING DELTA
TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported');
```

1. In that same [session](https://docs.databricks.com/aws/en/sql/user/queries/sessions.md), start a transaction and make a change:

   ```sql
   BEGIN TRANSACTION;
   INSERT INTO sample_accounts VALUES (10, 'Test', 100.00);
   ```

2. In a separate SQL Editor tab or notebook session (not a new cell in the same notebook), query the table:

   ```sql
   -- Run this in the SECOND session
   SELECT * FROM sample_accounts WHERE id = 10;
   ```

   This returns 0 rows because the uncommitted change is not visible outside your first session.

3. Return to your first session and commit:

   ```sql
   COMMIT;
   ```

4. Query from the second session again:

   ```sql
   -- Run this in the SECOND session
   SELECT * FROM sample_accounts WHERE id = 10;
   ```

   The row is visible because the transaction has been committed.

This isolation prevents other users from reading data that might be rolled back.

## <a id="choose" />Choose a transaction mode

| Scenario                                 | Recommended mode                                                        |
| ---------------------------------------- | ----------------------------------------------------------------------- |
| Scheduled ETL jobs                       | Non-interactive: automatic commit or rollback simplifies error handling |
| Fixed statement sequences                | Non-interactive: simpler syntax, no manual commit needed                |
| Data validation before commit            | Interactive: inspect results and decide whether to commit               |
| JDBC applications needing manual control | Interactive: standard database transaction patterns                     |

## <a id="additional-resources" /> Additional resources

* [Tutorial: Coordinate transactions across tables](https://docs.databricks.com/aws/en/transactions/tutorial.md)
* [Transactions](https://docs.databricks.com/aws/en/transactions/index.md)
* [Catalog commits](https://docs.databricks.com/aws/en/tables/features/catalog-commits.md)
* [Isolation levels and write conflicts](https://docs.databricks.com/aws/en/optimizations/isolation/index.md)
* [ATOMIC compound statement (non-interactive transactions)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic.md): Run multiple SQL statements as a single atomic transaction with automatic commit and rollback.
* [BEGIN TRANSACTION (interactive transactions)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-begin.md): Begin an interactive transaction with manual commit and rollback control.
* [COMMIT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-commit.md): Commit an interactive transaction and make all changes permanent.
* [ROLLBACK](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-txn-rollback.md): Roll back an interactive transaction and discard all changes.
