Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for Application Tier Database Transactions

I have seen a number of different cftransaction examples and read different sites and still have not been able to find a definitive answer to what parts of cftransaction are necessary. What I am trying to accomplish is very simple:

  • start a transaction
  • run multiple inserts/updates
  • close the transaction

If there is an error at any time during the inserts/updates, I want to rollback the transaction. Here is what I have done in the past:

<cftransaction>
 <cftry>
  <!--- multiple insert/update queries --->
 <cfcatch type="any">
  <cftransaction action="rollback">
  <!--- log error, show user message --->
 </cfcatch>
 </cftry>
<cftransaction action="commit">
</cftransaction>

Is this correct? What is the best practice? Not all of the instances of cftransaction are following the example above. Some only have the beginning and ending tags.

I have been seeing some issue with my database server and think that improper use of transactions could be one of the issues. I'm seeing a large number of: set transaction isolation level read committed processes in the activity monitor that are taking up a large amount of CPU.

like image 570
Jason Avatar asked Dec 04 '25 15:12

Jason


1 Answers

As you're doing it, the "commit" and "rollback" lines are redundant. They're really only useful if you want some of the queries committed and others rolled back. Since you're doing all or none you really don't need them.

I generally do this:

<cftry>
  <cftransaction>
    <!--- multiple insert/update queries --->
  </cftransaction>
  <cfcatch type="database">
    <!--- log error, show user message --->
  </cfcatch>
</cftry>
like image 68
ale Avatar answered Dec 08 '25 03:12

ale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!