Working with the Datasource Service Factory
I was playing around with the DataSource Service Factory today, and I found out how to do a few things. I created datasources, and used them, but I couldn't figure out how to get it to save the datasource. Anyways here's how you create a datasource, it took me a few tries to figure out the ordering of the setDatasource method (since reflection only tells you the datatypes, I had to figure out what each argument was for):
<cfset dsService = CreateObject("java", "coldfusion.server.ServiceFactory").DataSourceService>
<cfset dsManager = dsService.getDman()>
<cfset map = dsService.getDefaults()>
<!--- name, driver class, description, username, password, url, isPooled, timeout, interval, map --->
<cfset dsManager.setDatasource("datasource_name",
"macromedia.jdbc.MacromediaDriver",
"description",
"username",
"password",
"jdbc:macromedia:sqlserver://localhost:1433;databaseName=tester;SelectMethod=direct;sendStringParametersAsUnicode=false;MaxPooledStatements=1000",
true,
1200,
420,
map)>
The map variable has some more values you can set (mostly the permissions), but check out the default values by doing a cfdump of the map variable.
The above code will create a datasource that you can use, but it does not persist after the server is rebooted. If you can find a way to make that work please post a comment.
The next code example will list all the installed JDBC drivers, note that we are using the dsService variable from above.
<cfdump var="#dsService.getDrivers()#" label="Installed JDBC Drivers">
To test if a datasource exists:
<cfif dsManager.exists("datasource_name")>
it exists...
<cfelse>
it does not exist
</cfif>
To remove a datasource (not sure if this will persist after the server is restarted):
<cfset dsManager.removeDatasource("datasource_name")>
To list all installed datasources:
<table border="1"> <tr> <td>Name</td> <td>Description</td> <td>JDBC Class</td> <td>JDBC URL</td> <td>JDBC User</td> <td>JDBC Password</td> <td>Interval</td> <td>Pooled</td> <td>Timeout</td> </tr> <cfset nameArray = dsManager.getNames()> <cfloop from="1" to="#ArrayLen(nameArray)#" index="i"> <cfset name =nameArray[i]> <cfoutput> <tr> <td>#name#</td> <td>#dsManager.getDescription(name)#</td> <td>#dsManager.getJdbcClass(name)#</td> <td>#dsManager.getJdbcUrl(name)#</td> <td>#dsManager.getJdbcUsername(name)#</td> <td>#dsManager.getJdbcPassword(name)#</td> <td>#dsManager.getInterval(name)#</td> <td>#YesNoFormat(dsManager.isPooled(name))#</td> <td>#dsManager.getTimeout(name)#</td> </tr> </cfoutput> </cfloop>
To purge query cache:
<cfset dsService.purgeQueryCache()>
To verify a datasource (test connection):
<cfif dsService.verifyDatasource("datasource_name")>
verified
<cfelse>
not verified
</cfif>
As always remember that this undocumented stuff is subject to change from version to version.
Tweet
Trackbacks
- You italy2 Italy2
Comments
Andy http://www.creative-restraint.co.uk
Here's the permalink: http://www.creative-restraint.co.uk/blog/index.cfm?mode=entry&entry=7B83C13D-EEF8-5BAE-8195007923F93910
I've been looking around but can't seem to find a way
It does create a datasource, when I run a query, it says invalid URL.
The datasource created is not seen in the CF administrator (datasource management)
Thanks to help. Pierre.
Thanks!
Look into: CFIDE.adminapi.datasource
There you have a few methods such as : setMSSQL() and setOracle(), etc.
open the object in the Component Utility Tool and you will see what to pass to it, etc.
http://localhost/CFIDE/adminapi/datasource.cfc
Post a Comment
Recent Entries
- Nginx redirect www to non www domain
- HashDOS and ColdFusion
- HackMyCF Updated for APSB11-29 Security Hotfix
- Adobe eSeminar on FuseGuard
- Determining Which Cumulative Hotfixes are Installed on ColdFusion
- Adding Two Factor Authentication to ColdFusion Administrator
- ColdFusion Developer Week at Adobe.com
- Bug Loading Scripts for CFFileUpload and CFMediaPlayer


add to del.icio.us



