cftransaction

Instructs the database management system to treat multiple
database operations as a single transaction. Provides database
commit and rollback processing.
Note that distributed transactions (transactions across multiple datasources) are not supported - you must commit one transaction and begin a separate transaction to one database before writing a query to another (CFMX7 Manual)

  <cftransaction>

 transaction { }

Attribute Reference

action string
Default: begin

begin: the start of the block of code to execute
commit: commits a pending transaction
rollback: rolls back a pending transaction
setsavepoint: Marks a place within the transaction as a savepoint.
Values:
  • begin
  • commit
  • rollback
  • setsavepoint

isolation string

The isolation level for the transaction. It controls what this transaction can see of changes other transactions are making at the same time. When omitted, the database or driver default is used.

read_uncommitted: dirty reads, non-repeatable reads and phantoms can all occur. A dirty read is reading a row another transaction changed but has not yet committed.
read_committed: prevents dirty reads, but non-repeatable reads and phantoms can occur. A non-repeatable read is reading the same row twice and getting different data, because another transaction changed or deleted it and committed in between.
repeatable_read: prevents dirty and non-repeatable reads, but phantoms can occur. A phantom is running the same search twice and getting extra rows, because another transaction inserted matching rows in between.
serializable: prevents dirty reads, non-repeatable reads and phantoms. It is the strictest level, and the most likely to block or conflict with concurrent transactions.
Values:
  • read_uncommitted
  • read_committed
  • repeatable_read
  • serializable

savepoint string

The name of the savepoint in the transaction. Used with action="setsavepoint" or action="rollback"

nested boolean

specifies whether transaction is nested or not
Values:
  • true
  • false

Compatibility

Lucee:

Lucee does not support the savepoint attribute prior to 5.3.9

RustCFML:

isolation attribute parsed but never applied to the connection. cftransaction cannot span cfthread parent<->child boundary.

Examples
Sample code using the cftransaction tag


transaction { 
 try { 
 // code to run 
 transaction action="commit"; 
 } 
 catch(any e) { 
  transaction action="rollback"; 
  } 
 }

<cftransaction> 
 <cftry> 
 <!--- code to run ---> 
   <cftransaction action="commit" /> 
 <cfcatch type="any"> 
 <cftransaction action="rollback" /> 
 </cfcatch> 
 </cftry> 
 </cftransaction>

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub