CFTry and CFCatch to test database connection

Every once in a while our database server will be down, but the website server is fine. However, without a database, our application
is not very useful. So I use this little snippet to output a custom message. I use one for each datasource at the beginning of our app with an appropriate error message. CF8 has even more robust messaging capability for cfcatch if you want more.

<cftry>
<cfquery name="dbcheck" datasource="myDSN">
select top 5 * from TABLE
</cfquery>
<!--- specify the type of error for which we search --->
<cfcatch type = "Database">
<cfif #cfcatch.message# contains 'Error'>
<!--- the message to display --->
<p style="color: red">The application database is not avalilable.
<br /> Please try again in a few minutes. </p>
<cfabort>
</cfif>
</cfcatch>
</cftry>

Cup size   
Select size then click on coffee cup.
This entry was posted in Code, Coldfusion, Problems & Fixes, Snippets. Bookmark the permalink.