Did you know you could verify DSNs with ColdFusion code?

We are slowly wrapping lunch here at CFUnited Express, and the guy next to me brought up the issue of handling a database that is down. He was considering running a query wrapped in try/catch on every request, but that would be a bit slow. I did a quick check and was pleasantly surprised to see that DSN verification (the same thing you get in the ColdFusion Administrator) is available via the Admin API. Specifically, datasource.cfc, method verifyDSN. You can have it optionally return a message with error details if things go wrong. Here is a quick sample:

<cfscript> adminObj = createObject("component","cfide.adminapi.administrator"); adminObj.login("admin");

datasource = createObject(“component”, “cfide.adminapi.datasource”); res = datasource.verifyDSN(“peanutbutterjellytime”, true); </cfscript>

<cfdump var=”#res#”> </code>

Obviously the admin password would be different in production, but you get the idea. The result was:

coldfusion.sql.DataSourceFactory$DataSourceException: Datasource peanutbutterjellytime could not be found.

Setting the second argument to false makes the method just return a simple false.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments