Macromedia coldfusion 4.5-cfml language reference User Manual

Page of 608
Chapter 1:  ColdFusion Tags
221
Example
<!--- This example shows the use of CFTRANSACTION --->
<HTML>
<HEAD>
<TITLE>CFTRANSACTION Example</TITLE>
</HEAD>
<BODY>
<H3>CFTRANSACTION Example</H3>
<P>CFTRANSACTION can be used to group multiple queries
using CFQUERY into a single business event.  Changes to data
requested by these queries can be committed on the basis of the success 
or failure of the query by using the actions COMMIT and ROLLBACK, 
respectively.
<P>The following is a sample listing (see code in right pane):
<!----------------------------------------------------------------------
You can enclose the transaction in a CFTRY/CFCATCH block in order
to catch database errors and make commitment or rollback or the 
transaction contingent on the errors received.
----------------------------------------------------------------------->
<CFTRY>
<----------------------------------------------------------------------
Initialize commitIt to Yes.
----------------------------------------------------------------------->
<CFSET commitIt = "Yes">
<CFTRANSACTION ACTION="BEGIN">
<CFQUERY NAME=’makeNewCourse’ DATASOURCE=’cfsnippets’>
INSERT INTO Courses
(Number, Descript)
VALUES
(’#myNumber#’, ’#myDescription#’)
</CFQUERY>
<!------------------------------------------------------------------
Rolls back the pending insertion if database exception is caught.
------------------------------------------------------------------->
<CFCATCH TYPE="DATABASE">
<CFTRANSACTION ACTION="ROLLBACK"/>
<CFSET commitIt = "No">
</CFCATCH>
<CFIF commitIt>
<CFTRANSACTION ACTION="COMMIT"/>
<!--------------------------------------------------------------
Commits the pending insertion.
---------------------------------------------------------------->
<CFELSE>
<CFSET commitIt="Yes">
</CFIF>
<CFQUERY NAME=’insertNewCourseToList’ DATASOURCE=’cfsnippets’>
INSERT INTO CourseList
(CorNumber, CorDesc, Dept_ID,