For the complete documentation index, see llms.txt. This page is also available as Markdown.

Transactions

Database transactions are one of the most critical features for ensuring data integrity and consistency in your applications. BoxLang provides comprehensive transaction support through both a modern transaction{} block syntax and the underlying bx:transaction component.

🚀 Why Transactions Matter

Transactions ensure the ACID properties of database operations:

  • ⚛️ Atomicity: All operations succeed together or fail together - no partial updates

  • 🔒 Consistency: Database remains in a valid state before and after the transaction

  • 🏝️ Isolation: Concurrent transactions don't interfere with each other

  • 💾 Durability: Committed changes persist even after system failures

💡 Real-World Examples

// ❌ Without transactions - DANGEROUS!
queryExecute( "UPDATE accounts SET balance = balance - 100 WHERE id = 1" );
// 💥 What if the application crashes here?
queryExecute( "UPDATE accounts SET balance = balance + 100 WHERE id = 2" );

// ✅ With transactions - SAFE!
transaction {
    queryExecute( "UPDATE accounts SET balance = balance - 100 WHERE id = 1" );
    queryExecute( "UPDATE accounts SET balance = balance + 100 WHERE id = 2" );
    // Both updates succeed together or both are rolled back
}

📝 Transaction Syntax

BoxLang offers multiple ways to work with transactions:

The modern transaction{} block syntax provides automatic transaction management:

🔧 Transaction Attributes & Options

📋 Available Attributes

Attribute
Values
Description

action

begin, commit, rollback, setsavepoint, releasesavepoint

Action to perform on the transaction

isolation

read_uncommitted, read_committed, repeatable_read, serializable

Transaction isolation level

savepoint

String

Name of the savepoint to create, roll back to, or release

datasource

String

Specific datasource name for the transaction

🔒 Isolation Levels Explained

Choose the right isolation level based on your concurrency and consistency needs:

🎯 Transaction Behavior

🛜 Connections

In BoxLang transactions, no connection is acquired until the first JDBC query is executed. Consider this transaction block:

This block never acquires a JDBC connection. Savepoints requested before the first query are recorded as pending and materialized on the transaction connection before that query runs. Rollback and release of pending savepoints update the registry without opening a connection; commit, full rollback, and transaction end clear the registry. The operations still emit transaction events.

📢Events

BoxLang emits events during the lifecycle of a JDBC transaction which can be used to react to various transaction points in your app:

Read more about these events in transaction events.

🗄️ Datasources

In BoxLang, transactions are inherently single-connection concepts. You can't have a transaction that spans multiple connections or datasources.

Hence, despite containing TWO queries this transaction has only a single query that executes within a transaction context:

In Adobe and Lucee Server, the first query executed within the datasource determines the transactional datasource; that is, the first query to run sets the datasource to use for that transaction. Any queries which specify a different datasource will execute outside the context of the transaction.

To improve expectations around this behavior, BoxLang supports a datasource attribute on the transaction block:

Setting the datasource at the transaction block makes it much more obvious which datasource the transaction will operate upon.

💾 Savepoints

Savepoints allow you to create checkpoints within a transaction for partial rollbacks:

Savepoint lifetime

Savepoint names are case-insensitive and must be non-blank. A name cannot be created again while it is still registered. For the outer transaction:

Operation
Savepoints retained

transactionRollback( name )

The named savepoint and all earlier savepoints. Later savepoints are invalidated.

transactionReleaseSavepoint( name )

Only earlier savepoints. The named savepoint and all later savepoints are invalidated.

transactionCommit(), transactionRollback(), or transaction end

None.

Release savepoints after use

transactionReleaseSavepoint() and the releasesavepoint component action are added in BoxLang 1.19.0 (currently unreleased).

Release a savepoint once its recovery boundary is no longer needed. Release is optional cleanup; transaction atomicity and partial rollback do not require it. It does not commit or roll back changes.

In a batch import, release each item's savepoint after success or recovery so that completed boundaries do not accumulate until the outer transaction ends. This example assumes a configured default datasource and an existing products table:

A rollback keeps its target savepoint available, allowing the following release to discard it. The next iteration can reuse the same name. If a JDBC rollback or release fails, the error propagates out of the block and the outer transaction rolls back.

You can also release with the component action:

The statement-only component actions above require their terminating semicolons.

Once a connection exists, explicit release requires JDBC driver support. An unsupported or failed release raises a DatabaseException and leaves the registered handles available for recovery. Releasing a pending savepoint needs no connection. The BIF requires an active transaction; the component action is silently ignored when no transaction is active.

When nested transactions are enabled, child savepoints have their own namespaced ownership. Releasing a child savepoint cannot release the parent's savepoint with the same user-facing name. Automatic child completion does not require JDBC savepoint-release support.

🚨 Exception Handling

Transactions automatically roll back when exceptions occur:

📊 Performance Considerations

⚡ Best Practices

🔄 Deadlock Prevention

🔄 Transaction Events

See transaction events for a list of events that are triggered during transaction lifecycles such as begin, commit, rollback, and savepoint operations.

🏗️ Nested Transactions

By default, enableNestedTransactions is false, so inner transaction{} blocks share the outer transaction context. A full rollback inside an inner block rolls back the outer transaction as well.

When enableNestedTransactions is enabled, BoxLang uses child savepoints to implement nested blocks on the same JDBC connection. These are not independent database transactions: an outer rollback can still undo child work. Child savepoint names are isolated, and explicit release cannot release a parent savepoint. The following example shows the default behavior with nested transactions disabled.

🧰 Transactional BIFs

See our list of transactional BIFs:

🎨 Common Patterns

💰 Financial Transfers

🛒 E-commerce Order Processing

🔄 Batch Data Processing

🔗 Related: For optimal performance, consider using connection pooling and properly configured query caching alongside your transaction management strategy.

Last updated

Was this helpful?