Macromedia coldfusion 4.5-cfml language reference User Manual
Chapter 1: ColdFusion Tags
221
Example
<!--- This example shows the use of CFTRANSACTION --->
<HTML>
<HEAD>
<TITLE>CFTRANSACTION Example</TITLE>
</HEAD>
<HEAD>
<TITLE>CFTRANSACTION Example</TITLE>
</HEAD>
<BODY>
<H3>CFTRANSACTION Example</H3>
<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):
<!----------------------------------------------------------------------
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.
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">
<CFTRY>
<----------------------------------------------------------------------
Initialize commitIt to Yes.
----------------------------------------------------------------------->
<CFSET commitIt = "Yes">
<CFTRANSACTION ACTION="BEGIN">
<CFQUERY NAME=’makeNewCourse’ DATASOURCE=’cfsnippets’>
INSERT INTO Courses
INSERT INTO Courses
(Number, Descript)
VALUES
(’#myNumber#’, ’#myDescription#’)
</CFQUERY>
<!------------------------------------------------------------------
Rolls back the pending insertion if database exception is caught.
------------------------------------------------------------------->
<CFCATCH TYPE="DATABASE">
Rolls back the pending insertion if database exception is caught.
------------------------------------------------------------------->
<CFCATCH TYPE="DATABASE">
<CFTRANSACTION ACTION="ROLLBACK"/>
<CFSET commitIt = "No">
<CFSET commitIt = "No">
</CFCATCH>
<CFIF commitIt>
<CFIF commitIt>
<CFTRANSACTION ACTION="COMMIT"/>
<!--------------------------------------------------------------
Commits the pending insertion.
---------------------------------------------------------------->
<!--------------------------------------------------------------
Commits the pending insertion.
---------------------------------------------------------------->
<CFELSE>
<CFSET commitIt="Yes">
</CFIF>
<CFQUERY NAME=’insertNewCourseToList’ DATASOURCE=’cfsnippets’>
INSERT INTO CourseList
INSERT INTO CourseList
(CorNumber, CorDesc, Dept_ID,