# CFTRANSACTION

Use CFTRANSACTION to group multiple queries into a single unit. CFTRANSACTION also provides commit and rollback processing. See Usage for details.

### Syntax

    <CFTRANSACTION
        ACTION="BEGIN" or "COMMIT" or "ROLLBACK"
        ISOLATION="Read_Uncommitted" or
                     "Read_Committed" or
                     "Repeatable_Read" >
    </CFTRANSACTION>

#### ACTION

Optional. The actions are as follows:

- BEGIN, which indicates the start of the block of code to be executed. It is the default value.
- COMMIT, which commits a pending transaction.
- ROLLBACK, which rolls back a pending transaction.

#### ISOLATION

Optional. ODBC lock type. Valid entries are:

- Read_Uncommitted
- Read_Committed
- Repeatable_Read
- Serializable

### Usage

A transaction block is created within these tags:

    <CFTRANSACTION>
        queries to be executed
    </CFTRANSACTION>

Within the transaction block, you can commit a transaction by nesting the <CFTRANSACTION ACTION="COMMIT"/> tag or roll the transaction back by nesting the <CFTRANSACTION ACTION="ROLLBACK"/> tag within the block.

Within one transaction block, you can write queries to more than one database; however, you must commit or rollback the transaction to a particular database prior to writing a query to another database.

By using CFML error handling, you have control over whether each transaction is to be committed based on the success or failure of the database query.

Use the ISOLATION attribute for additional control over how the database engine performs locking during the transaction.

### 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,
            CorName, CorLevel, LastUpdate)
        VALUES
            ('#myNumber#', '#myDescription#', '#myDepartment#',
            '#myDescription#', '#myCorLevel#', #Now()#)
        </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>
    </CFTRANSACTION>
    </CFTRY>

    </BODY>
    </HTML>