• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1281
  • Last Modified:

proper use of <cftransaction> with <cftry> and <cfcatch>

It may seem that I am splitting hairs but I would like to get people's opinion on this.  While I am almost sure that both structures below are practically the same I wouldn't be able to say which is more correct.  Should you put a <cftransaction> inside a <cftry> or the other way around?  Are they exactly the same or can there be a case when they are not?

<CFTRANSACTION ACTION="BEGIN">
   <CFTRY>
     <!---cfquery NO:1--->

     <!---cfquery NO:n..n.n--->

   <CFCATCH type="Database">
      <cftransaction Action="Rollback"/>
      <CFABORT showerror="There was an error saving your data: #CFCATCH.Message#: #CFCATCH.Detail#">
   </cfcatch>
   </cftry>
</cftransaction>


<CFTRY>
   <CFTRANSACTION>
      <!---cfquery NO:1--->

      <!---cfquery NO:n..n.n--->
   </CFTRANSACTION>

   <CFCATCH type="Database">
      <cftransaction Action="Rollback"/>
      <CFABORT showerror="There was an error saving your data: #CFCATCH.Message#: #CFCATCH.Detail#">
   </cfcatch>
</CFTRY>
0
bisonfur37
Asked:
bisonfur37
3 Solutions
 
Tacobell777Commented:
Personally I put the transaction outside the cftry, but they both work the same...
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi bisonfur37!

You may refer on the following livedocs to have a clear understanding of what really its usage :

1.  <cftry> - http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-c18.htm#wp1104557
2.  <cfcatch> - http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-a10.htm#wp3395038
3.  <cftransaction> - http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-c15.htm#wp1104164

Hope this helps you.


Regards!
eNTRANCE2002 :-)
0
 
bisonfur37Author Commented:
Great tips, actually I had already read those.  The question still remains.  How do you properly use <CFTRANSACTION> with <CFTRY> and <CFCATCH> to provide the most user friendly errors?  <CFTRANSACTION> does a great job of not messing things up on the dB side.  Now, I just want to provide the user with nice messages should things go South.  I am almost positive that the syntax would be as follows:

<CFTRY>
   <CFTRANSACTION>
      <!---cfquery NO:1--->

      <!---cfquery NO:n..n.n--->
   </CFTRANSACTION>

   <CFCATCH type="Database">
      <cftransaction Action="Rollback"/>
      <CFABORT showerror="There was an error saving your data: #CFCATCH.Message#: #CFCATCH.Detail#">
   </cfcatch>
</CFTRY>

And that you would most likely want to have a single <CFTRANSACTION> within a <CFTRY>.  Does anyone have anything to say about this?  Does anyone see anything wrong, or at least not efficient/recommended, with having multiple <CFTRANSACTIONS> within <CFTRY>?
0
 
pinaldaveCommented:
Personally I will do same as TacoBell.
Both are same and will have same effect. Very interesting Q. I think they are same thing. (What a Q? There is nothing Experts can think of. good one!)
Regards,
---Pinal
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now