Question

I am using the cftransaction tag and when error is coming, it is not rolling back the entry, i am not sure what is wrong, I am not using the nested part of cftransaction yet, simple one is not working, i am not sure how nested will behave here, correct my code if it is wrong guys...

<cffunction access="public" name="newRegistration" returntype="any">
    <cfargument name="structform" default="" required="no" type="struct">
      <cfset var newregis = "">
      <cfset var str = "">
      <cfset var msg = "">
      <cftry>
        <cftransaction action="begin"/>
           <cfquery name="newregis" result="lastGenerated">
               --- Insert Goes Here ---
           </cfquery>
           <cfset rsLastID = lastGenerated.generated_key>
           <cfif isDefined('arguments.structform.mailoption') AND arguments.structform.mailoption EQ 'Yes'>
               <cfset msg = createAccountEmail(rsLastID,'nar')>
           </cfif>
           <cftransaction action="commit"/>
           <cfset str = "Account Created Successfully. #msg#">
           <cfcatch type="any">
             <cftransaction action="rollback"/>
             <cfset str = "Error! #cfcatch.Detail# #cfcatch.Message#. Entry rolled back, try again">
           </cfcatch>
         </cftry>
         <cfreturn str>
</cffunction>
Was it helpful?

Solution

You appear to be missing the closing CFTRANSACTION tag which should be wrapped around the try/catch block.

<cftransaction action="begin">
    <cftry>
        ...
        <cfcatch type="any">
            <cftransaction action="rollback" />
            ...
        </cfcatch>
    </cftry>
</cftransaction>

Use that structure and you should be good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top