View comments | RSS feed

cftransaction

Description

Groups database queries into a unit. Provides database commit and rollback processing.

Category

Database manipulation tags

Syntax

<cftransaction 
  action = "begin" or "commit" or "rollback"
  isolation = "read_uncommitted" or "read_committed" or 
    "repeatable_read" >
</cftransaction>

See also

cfinsert, cfprocparam, cfprocresult, cfquery, cfqueryparam, cfstoredproc, cfupdate

Attributes

Attribute Req/Opt Default Description
action
Optional
begin
  • begin: the start of the block of code to execute.
  • commit: commits a pending transaction
  • rollback: rolls back a pending transaction
isolation
Optional

ODBC lock type.
  • read_uncommitted
  • read_committed
  • repeatable_read
  • serializable

Usage

Within a transaction block, you can do the following:

(In these examples, the slash is alternate syntax that is the equivalent of an end tag.)

Within a transaction block, you can write queries to more than one database, but you must commit or rollback a transaction to one database before writing a query to another. Using CFML error handling, you control whether each transaction is committed, based on the success or failure of the database query. To control how the database engine performs locking during the transaction, use the isolation attribute.

Example

<p>CFTRANSACTION can be used to group multiple queries that use CFQUERY
into one business event.  Changes to data that is requested by the queries
are not committed to the datasource until all actions within the transaction 
block have executed successfully.
<p>This a view-only example. 
<!--- 
<cftransaction>
  <cfquery name='makeNewCourse' datasource='Snippets'>
  INSERT INTO Courses
    (Number, Descript)
  VALUES
    ('#myNumber#', '#myDescription#')
  </cfquery>

  <cfquery name='insertNewCourseToList' datasource='Snippets'>
  INSERT INTO CourseList
    (CorNumber, CorDesc, Dept_ID,
    CorName, CorLevel, LastUpdate)
  VALUES
    ('#myNumber#', '#myDescription#', '#myDepartment#',
    '#myDescription#', '#myCorLevel#', #Now()#)
  </cfquery>
</cftransaction> 
--->

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


earnshavian said on Dec 10, 2003 at 7:50 AM :
CFTRANSACTION and TRANSACTIONs created in SQL Server stored procedures should only be used to encapsulate code performing UPDATEs, INSERTs and DELETEs. This means that any SELECT statements should be outside of your TRANSACTIONs to avoid locking rows unnecessarily. If your stored procs are called from within a CFTRANSACTION, you should avoid unnecessary SELECTs or move the encapsulation into your SQL Server Stored Procedure and hence move the business logic.

The consequence of failing to do this is slower code since it is waiting for row and table locks to be released under small load. It can also lead to blocking on your server.
ThisIsMe said on May 20, 2004 at 9:06 AM :
This is not a very good example of how to emply CFTRANSACTION, it does not show how to use the various actions.. Its pretty much a shot in the dark unless you get the livedocs for CF5 (which is more detailed) but following the code there is invalid because cftry requires the last tag executed within itself be cfcatch.. so the method of catching and setting a variable in that method doesn't work.. This is how you need to do it..

<Cfset gotrue=1>
<cftransaction>
<cftry>...queries here...
<cfcatch>...catch the error...
<cfset gotrue=0>
<cftransaction action="rollback">
<cfrethrow> <!---This line throws the error back to your error handler if you have one.. If you don't, you can remove this...--->
</cfcatch>
</cftry>
<cfif gotrue eq 1>
...do something...
<cftransaction action="commit">
</cfif>
</cftransaction>

Livedocs really needs to improve this page, very badly...

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt315.htm