{"id":397,"date":"2010-09-28T12:13:36","date_gmt":"2010-09-28T16:13:36","guid":{"rendered":"http:\/\/erics-test-lab.com\/?p=397"},"modified":"2010-09-28T12:13:36","modified_gmt":"2010-09-28T16:13:36","slug":"cftry-and-cfcatch-to-test-database-connection","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2010\/09\/cftry-and-cfcatch-to-test-database-connection\/","title":{"rendered":"CFTry and CFCatch to test database connection"},"content":{"rendered":"<p>Every once in a while our database server will be down, but the website server is fine. However, without a database, our application<br \/>\nis 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.<\/p>\n<p><code>&lt;cftry&gt;<br \/>\n  &lt;cfquery name=\"dbcheck\" datasource=\"myDSN\"&gt;<br \/>\n       select top 5 * from TABLE<br \/>\n  &lt;\/cfquery&gt;<br \/>\n  &lt;!--- specify the type of error for which we search ---&gt;<br \/>\n  &lt;cfcatch type = \"Database\"&gt;<br \/>\n      &lt;cfif #cfcatch.message# contains 'Error'&gt;<br \/>\n  &lt;!--- the message to display ---&gt;<br \/>\n    &lt;p style=\"color: red\"&gt;The application database is not avalilable.<br \/>\n    &lt;br \/&gt; Please try again in a few minutes. &lt;\/p&gt;<br \/>\n    &lt;cfabort&gt;<br \/>\n   &lt;\/cfif&gt;<br \/>\n &lt;\/cfcatch&gt;<br \/>\n&lt;\/cftry&gt;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.esearing.com\/v2010\/2010\/09\/cftry-and-cfcatch-to-test-database-connection\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,16,21,24],"tags":[],"class_list":["post-397","post","type-post","status-publish","format-standard","hentry","category-code","category-coldfusion","category-problems-fixes","category-snippets-quick-code-tips-for-single-elements-attributes-or-details"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=397"}],"version-history":[{"count":0,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/397\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}