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

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 or rollback to

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 transaction is a no-op. It begins, tries to set a savepoint, then roll back to the savepoint, then commit... but never ran any JDBC queries. Hence, every transactional BIF called above does exactly nothing (besides emit 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:

🚨 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

BoxLang does not support true nested transactions. If you call transaction{} blocks within another transaction{} block, they will share the same transaction context. This means that if an inner block rolls back, it will roll back the entire transaction, including any operations performed in the outer block.

🧰 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?