One of the cool things with CFML is how it makes accessing databases so very painless and easy. Assuming of course, that you have the datasource already setup. It's ability to dynamically create database connections has always been somewhat hazy at best. A number of backdoors to the Admin API has been given to allow people the ability to add new datasources to the underlying engine. But, what if you only want to temporarily use it?
I have had such a need for some time now. We run a number of large installations in the cloud and as a way of tracking of who-is-what and where-what-is, we use Amazon's SimpleDB service to act as a global registry for us. When an instance startups, it registers its connection details into a given SimpleDB domain and what its intended role in life is. All the other components within the system then utilise this SimpleDB registry to figure out how to connect to a given service.
I wanted my CFML apps to be able to play in this global registry lookup game. OpenBD has already had deep Amazon SimpleDB deep integration, but what we didn't have was a nice clean way of creating a datasource, using it, and then disposing of it.
I didn't want to go near the Admin API of OpenBD, as these datasources were purely dynamic and disposable. I opted to therefore create a few extra core functions that would allow me to achieve what I wanted.
Let's look at a quick example of these functions in use. Here I create a new datasource, called temp and register it. At this point, this datasource is now available throughout the whole engine and will remain available until the engine is restarted or someone calls DataSourceDelete() on it.
<cfset s = StructNew()> <cfset s.hoststring = "jdbc:mysql://192.168.100.236:3306/MyDB"> <cfset s.drivername = "com.mysql.jdbc.Driver"> <cfset s.databasename = "MyDB"> <cfset s.username = "username"> <cfset s.password = "mypassword"> <cfif NOT DataSourceIsValid("temp")> <cfset DataSourceCreate( "temp", s )> <p>Registered Datasource</p> </cfif>
Once registered, you can now use the datasource as a first class citizen with all the favourite tags/functions for accessing databases. Note though, you can't delete datasources that are definied using the bluedragon.xml method.
This example only shows the minimum amount of data required to create a datasource. If you look at the documentation for DataSourceCreate() then you can set every parameter you can as if you were hardcoding it in the bluedragon.xml or using the Admin API. The only difference is that this datasource is not persisted.
These functions now make it easier to divorce the CFML application away from the underlying platform even further. No longer does your installation need prior knowledge of the databases it is to be utilising. Instead you can store this data elsewhere, in configuration files, or global registries like Amazon's SimpleDB.
This functionality is available now in the nightly build of OpenBD.