‘this.datasource’ is awesome in ColdFusion 11

Normally In ColdFusion applications we set up the datasources in ColdFusion Administrator or using the Admin API.So this makes very difficult for our Application to be portable across different ColdFusion server. Because while migrating to another ColdFusion server first of all we have to recreate all the datasources again.

(Note: Once I have tried to back up all my datasources using the neo-datasource.xml file(which stores all datasource info) of my old CF server. But that does not works because of the password encryption/decryption issues.By password encryption issues, I mean the database password that we give while creating a datasource, this password gets stored in encrypted format in our ColdFusion server.So if we migrate our neo-datasource.xml file from Old CF server to new CF server , The new CF server can not decrypt those passwords properly.)

ColdFusion 11 brings a new features for creating datasources in application level instead of messing up with CF Admin.
We can define all our datasource details in Application.cfc.

 this.datasources.dsn1 = {  "database"="regression",
                   "host"="localhost\MSSQL2008", 
                   "driver"="MSSQLServer", 
                   "username"="sa", 
                   "password"="mindfire" };
 
 this.datasources.dsn2 = {   "driver"="MSSQLServer", 
                   url="jdbc:macromedia:sqlserver://localhost\MSSQL2008; 
                   databaseName=regression; 
                   sendStringParametersAsUnicode=false; 
                   querytimeout=0; 
                   MaxPooledStatements=1000", 
                   "username"="sa", 
                   "password"="mindfire"};  

If you are using built in DB drivers that are shipped with ColdFusion , then just specify the driver name and the corresponding driver class will be picked up by ColdFusion. In neo-drivers.xml, you can find all the possible values for the driver.

But if you are using your own custom driver, you can use this syntax.

this.datasources.customDSN = { "driver" = "other", 
                 "url" = "jdbc:sqlserver://localhost\MSSQL2008; 
                 databaseName=pubs; 
                 sendStringParametersAsUnicode=false; 
                 querytimeout=0; 
                 MaxPooledStatements=1000", 
                 "username" = "sa", 
                 "password" = "password", 
                 "class" = "com.microsoft.sqlserver.jdbc.SQLServerDriver"}; 

Note: But If there is a server wide datasource with the same name exist, then application level datasource will be overridden by server level datasource.

Advertisements