The ColdFusion MX 7 Admin API can be used to create new datasources or modify existing ones in a supported, programmatic manner. Its useful to verify the datasource once created or modified, however the Admin API does not provide the error detail should verification fail, rather it only provides a boolean true or false result.

To obtain error details for failed datasource verification for a datasource created or modified through the Admin API, the ServiceFactory's DataSourceFactory can be invoked to call its verifiyDatasource() method instead. While this is not an optimal solution since the ServiceFactory methods remains unsupported, the Admin API is essentially a supported wrapper for the ServiceFactory anyway and much has been written about using ServiceFactory to manipulate ColdFusion server configuration. Here, the use of ServiceFactory is read-only, and is not used to make changes, but simply to verify a datasource to get to the error.

One bug to watch out for is that the timeout value for a new datasource is set in seconds, although when browsing the generated CFC metadata for the Admin API the timeout is described incorrectly as minutes.

view plain print about
2    request.CFADMIN_PASSWORD = "myPassword";
3    request.adminObj = createObject("Component", "cfide.adminapi.administrator");
4    request.adminObj.login(request.CFADMIN_PASSWORD);
5    request.dsnObj = createObject("component", "cfide.adminapi.datasource");
8    aDatasource = structNew();
9    aDatasource.DATA_SRC_NAME = "oracleXE10g_adminapi_1";
10    aDatasource.SRVR_NAME = "localhost";
11    aDatasource.SID_NAME = "XE";
12    aDatasource.PORT_NBR = 1521;
13    aDatasource.CON_TMOT = 500;
14    aDatasource.MAX_CON = 25;
15    aDatasource.MAX_POOL_STAT = 250;
16    aDatasource.APPL_USER_ID = "cfuser";
17    unencryptedpassword = "coldfusion";
18    disabled = false;
20[cfset request.dsnObj.setOracle(
21    name="#ucase(aDatasource.DATA_SRC_NAME)#",
22    host="#aDatasource.SRVR_NAME#",
23    port="#aDatasource.PORT_NBR#",
24    sid="#aDatasource.SID_NAME#",
25    username="#aDatasource.APPL_USER_ID#",
26    password="#unencryptedpassword#",
27    timeout="#aDatasource.CON_TMOT#",
28    enablemaxconnections = true,
29    maxconnections="#aDatasource.MAX_CON#",
30    disable="#disabled#",
31    maxPooledStatements="#aDatasource.MAX_POOL_STAT#")> <br/><br/>
32[cfset verified = request.dsnObj.verifyDSN(aDatasource.DATA_SRC_NAME)><br/><br/>
33[cfif not verified>
34    [cftry>
35        [cfscript>
36         factory = createObject("java", "coldfusion.server.ServiceFactory");
37         request.sqlexecutive = factory.getDataSourceService();
38         success = request.sqlexecutive.verifyDatasource(aDatasource.DATA_SRC_NAME);
39        [/cfscript>
40    [cfcatch>
42        [cfdump var="#cfcatch#" label="ERRORS">
43    [/cfcatch>
44    [/cftry>
46    <h3>Yipee! It verified :)</h3>