[skratchdot.]

Recently I was asked how to programmatically verify ColdFusion DataSources. I came up with a few methods of doing so. Each have their pros and cons.

Method #1 : Try/Catch using cfquery

PROS:

  • Can test for “datasource” specific behavior by using a custom cfquery (ie. “SELECT 1” vs. “SELECT name FROM Customers”). If you do this, the query may not work for other datasources, thereby not actually testing the validity of the datasource (a CON).

CONS:

  • Uses try/catch.
  • Does not work with all datasources. I don’t know of a cfquery that will work for all datasources / DBs / etc

NOTES:

This is my least favorite, because I couldn’t come up with a cfquery to test all datasources. It will work for some datasources, but not all.

SOURCE:

 1 <cffunction name="verifyDsnList1" output="true" returntype="void">
 2 	<cfargument name="list" type="string" required="true" />
 3 	<cfargument name="delimiter" type="string" required="false" default="," />
 4 	<cfset var local = StructNew() />
 5 	<cfoutput>
 6 		<hr />
 7 		<b><u>veriftyDsnList1</u></b>
 8 		<cfloop list="#arguments.list#" index="local.currentName">
 9 			<cftry>
10 				<hr />
11 				<cfquery name="local.qVerifyDatasource" datasource="#local.currentName#">
12 					SELECT 1
13 				</cfquery>
14 				<cfif local.qVerifyDatasource.RecordCount gt 0>
15 					DATASOURCE: #local.currentName# [VERIFIED=true]
16 				<cfelse>
17 					DATASOURCE: #local.currentName# [VERIFIED=false]
18 				</cfif>
19 				<cfcatch>
20 					DATASOURCE: #local.currentName# [VERIFIED=false] [ERROR: #cfcatch.message#]
21 				</cfcatch>
22 			</cftry>
23 		</cfloop>
24 		<hr />
25 	</cfoutput>
26 </cffunction>

Method #2: Try/Catch using the DataSourceService verifyDatasource() method.

PROS:

  • Will work for all datasources.
  • Shows “error” specific messaging that might help debug why a datasource isn’t working.

CONS:

  • Uses try/catch.

SOURCE:

 1 <cffunction name="verifyDsnList2" output="true" returntype="void">
 2 	<cfargument name="list" type="string" required="true" />
 3 	<cfargument name="delimiter" type="string" required="false" default="," />
 4 	<cfset var local = StructNew() />
 5 	<cfset local.dsService = CreateObject("java", "coldfusion.server.ServiceFactory").DataSourceService />
 6 	<cfoutput>
 7 		<hr />
 8 		<b><u>veriftyDsnList2</u></b>
 9 		<cfloop list="#arguments.list#" index="local.currentName">
10 			<cftry>
11 				<hr />
12 				<cfif local.dsService.verifyDatasource(local.currentName)>
13 					DATASOURCE: #local.currentName# [VERIFIED=true]
14 				<cfelse>
15 					DATASOURCE: #local.currentName# [VERIFIED=false]
16 				</cfif>
17 				<cfcatch>
18 					DATASOURCE: #local.currentName# [VERIFIED=false] [ERROR: #cfcatch.message#]
19 				</cfcatch>
20 			</cftry>
21 		</cfloop>
22 		<hr />
23 	</cfoutput>
24 </cffunction>

Method #3: Admin API verifyDSN() call

PROS:

  • Will work for all datasources.
  • Doesn’t use try/catch.

CONS:

  • Need to pass in cfide password. This should never be hardcoded, or kept in plain text.
  • Will never output “error” specific text (only ever displays true or false).

SOURCE:

 1 <cffunction name="verifyDsnList3" output="true" returntype="void">
 2 	<cfargument name="cfide_password" type="string" required="true" />
 3 	<cfargument name="list" type="string" required="true" />
 4 	<cfargument name="delimiter" type="string" required="false" default="," />
 5 	<cfset var local = StructNew() />
 6 	<cfset local.admin = createObject("component","cfide.adminapi.administrator").login(arguments.cfide_password) />
 7 	<cfset local.dsObj = createObject("component","cfide.adminapi.datasource") />
 8 	<cfoutput>
 9 		<hr />
10 		<b><u>veriftyDsnList3</u></b>
11 		<cfloop list="#arguments.list#" index="local.currentName">
12 			<hr />
13 			DATASOURCE: #local.currentName# [VERIFIED=#local.dsObj.verifyDSN(local.currentName)#]
14 		</cfloop>
15 		<hr />
16 	</cfoutput>
17 </cffunction>

Helper Function:

Here’s a helper function that will return a sorted list of all configured datasource names:

1 <cffunction name="getDatasourceList" output="false" returntype="string">
2 	<cfargument name="sort_type" type="string" required="false" default="textnocase" hint="Optional. See livedocs for ListSort()." />
3 	<cfargument name="sort_order" type="string" required="false" default="asc" hint="Optional. See livedocs for ListSort()." />
4 	<cfargument name="delimiter" type="string" required="false" default="," hint="Optional. See livedocs for ListSort()." />
5 	<cfset var dsService = CreateObject("java", "coldfusion.server.ServiceFactory").DataSourceService />
6 	<cfset var sDatasources = dsService.getDatasources() />
7 	<cfset var dsList = StructKeyList(sDatasources, arguments.delimiter) />
8 	<cfreturn ListSort(dsList, arguments.sort_type, arguments.sort_order, arguments.delimiter) />
9 </cffunction>

Testing all 3 methods:

Here’s a small script to test all the functions from this post. For this to work correctly, you’ll need to set the correct CFIDE password.

 1 <cfset my_cfide_password = "admin" />
 2 <cfset my_datasource_list = getDatasourceList() />
 3 
 4 <cfoutput>
 5 	#verifyDsnList1(my_datasource_list)#
 6 	<br />
 7 	#verifyDsnList2(my_datasource_list)#
 8 	<br />
 9 	#verifyDsnList3(my_cfide_password, my_datasource_list)#
10 	<br />
11 </cfoutput>
Tagged with: adminapiColdFusiondatasources


blog comments powered by Disqus