Using CFTry To Log A Failed CFTransaction

Published: {ts '2012-01-03 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/using-cftry-to-log-a-failed-cftransaction/

Im currently in the process of converting my site from Fusebox to FW1 so this is going to be a short post on a technique I learned a few years ago but I still think its useful especially for new developers.

Using CFTransaction To Roll Back Failed Query Groups

Up to a few years ago I did all my data modification in stored procedures which of course allows you to do transactions. However when I switched to writing my queries at the application level I discovered that CF also allowed for transactions with the <cftransaction> tag. (CF just rocks). One problem that I had though was that if a transaction failed there was no direct way of logging the failure.

To demonstrate take the following code block. Every time this is run the first query tries to do an insert only to be rolled back by the failure of the second query.

INSERT into table_1 (test1) values ('hello') INSERT into table_1 (iDontExist) values ('hello')

While the above is fine as far as data consistency we still need to be able to detect and handle the fail at the application level.

Combining CFTry and CFTransaction

In order to handle a failed transaction at the application level we simply pair the cftransaction tag with a cftry as follows.

INSERT into table_1 (test1) values ('hello') INSERT into table_1 (iDontExist) values ('hello') #message#

When you run this code snippet every time it fails the user gets a message to go shout at Steven until he fixes the bad query and then all is right with the world.

A Note To Railo Users

This technique works on Adobe CF and Railo (I haven't tried Openbd but I'd be surprised if it didn't) but in Railo you have to self close the commit & rollback <cftransaction> tags. This caught me out when I first time I migrated a site to Railo.