Skip to main content
Participant
January 4, 2011
Question

CFTRANSACTION completed?

  • January 4, 2011
  • 3 replies
  • 1381 views

Short of querying the DB and counting records, is there a way to determine if <CFTRANSACTION> has successfully completed?

    This topic has been closed for replies.

    3 replies

    Participant
    January 5, 2011

    More concisely, I meant is there a way for CFTRANSACTION to return a successful result. Like CFHTTP, etc.

    What I've come up with is pretty awkward:

    <cffunction name="someFunc">


         <cfset var bSuccess=true>


         <cftransaction>

              <cftry>

                   <cfquery></cfquery>

                   <cfquery></cfquery>

                   <!--- etc --->

              <cfcatch>

                   <cftransaction action="rollback">

                   <cfset bSuccess=false>

               </cfcatch>

              </cftry>

         </cftransaction>


         <cfreturn bSuccess>


    </cffunction>

    All of this seems a bit forced to me; just wondering if there was a more elegant way.

    Owainnorth
    Inspiring
    January 5, 2011

    No, you're pretty much there. There are two ways, depending on whether you're of the "functions should only have one return statement" or the "functions should return as soon as they're completed" school of thinking. I'm the latter, so:

    <cffunction name="someFunc">

        <cftransaction>

            <cftry>

                <cfquery></cfquery>

                <cfquery></cfquery>

                <!--- etc --->

                <cftransaction action="commit" />

                <cfreturn true />

            <cfcatch>

                <cftransaction action="rollback" />

                <cfreturn false />

            </cfcatch>

            </cftry>

        </cftransaction>

    </cffunction>

    Although incidentally this has nothing to do with cftransaction returning a result any more than a cfif can return a result, it's just making sure the usual flow control rolls back your transaction when required, and that the function returns the correct result. A commit *cannot* fail, so if something's gone wrong it will have done so before the commit, and so the transaction will be rolled back.

    Inspiring
    January 4, 2011

    I would normally assume that SQL statements run inside a CFTRANSACTION block will be committed so long as they do not throw an exception.  Do you have a specific scenario in mind here?  Please post your code and information about your database (vendor, version, table structure) and CF web server (CF version) if you need more information.

    Inspiring
    January 4, 2011

    I would turn this around the other way.  Unless you get an error, the transaction will have been completed by either the </cftransaction> tag or a <cftransaction> tag with a specific action (eg: COMMIT, or ROLLBACK).

    --

    Adam