Join Daniel Short for an in-depth discussion in this video Setting the default data source and SMTP, part of ColdFusion 9 New Features.
- View Offline
In this video, I'm going to show you two small but very powerful productivity enhancements in ColdFusion 9, being able to set a default data source and SMTP servers for an entire application. What this means is that in my Application.cfc, I can specify a data source and an SMTP server that get applied to all CF queries and <cfmail> tags in my application if they don't already have that attribute applied. In my Chapter2 Exercise Files folder, we'll open defaultdatasource.cfm and you can see I have a CFQuery that just selects * FROM Artists from the cfartgallery data source.
If I preview that, you can see it's dumping all of my artists to the page. But it is a pain to always put the datasource attribute on every single CFQuery. Normally, you get around this in ColdFusion 8 applications by storing that value in some sort of application scope variable and applying that to every single CFQuery. While that works, there's one more thing that you have to remember to add to every single <cf query> tag. So, I'm going to remove this entirely. Now if I preview this file, everything falls apart. There is no data source specified.
But if I go to my Application.cfc file, I can now into this scope set this.datasource equals cfartgallery. Now if I go back to default datasource.cfm and preview, I see all of my Artists once again. So, because this CFQuery didn't have a datasource attribute, it inherited it from the value specified in the Application.cfc. Now one thing to keep in mind is that the queries inherit the data source from the application that is calling them, not the application they actually live in.
To show you that, I have an ArtistService.cfc. Now this ArtistService does exactly the same as the other page. It just selects * FROM Artists and it does not have a data source specified. And then in my App2 folder, I have an Application.cfc and it currently has its datasource set to cfartgallery, and showArtists.cfm calls the ArtistService, gets all of the Artists and then dumps them to the page. This page works especially fine because in the Applications.cfc, I have set the datasource to cfartgallery Now if I change the datasource to cfbookclub, I should get an error because Artists table does not exist in the bookclub database.
So I'll go back to showArtists and run the page and we can see that the table, Artists, does not exist. So as you can see I called ArtistService, which is outside of the App2 application, but it did use the data source from this Application.cfc. So as you call external resources, remember that every file that you call is going to use the defaults from the calling application. So the SMTP servers work the same way. I am going to close a couple of files here and the only file I have left open is my Application.cfc here in Chapter2.
So I am going to open the smtpserversettings.cfm and as you can see, I have a <cfmail> tag here with a subject, server, username, and password. So I am sending this mail through my own mail server to my Gmail account. So let's go ahead and preview this in Safari and we are going to see a totally blank page. But if I switch over to my Gmail account, you can see I already have an email that showed up in my Inbox. Now if I go back to CF Builder and remove this information, I'm just going to cut it to my clipboard, save it and preview the file again, I'm going to get an error that says that no SMTP server was specified.
Again, this is something where I would have to go through and every single <cfmail> tag would have to have that server, username, and password information specified. But in CF 9, if I go over to the Application.cfc, I can do the same thing I did with the datasource. I can do cfset this.smtpserversettings equals and I can specify a structure which defines all of that information that I need to supply to the <cfmail> tag. Let's just paste in those attributes I copied and separate them with commas.
Now I've set this.smtpserversettings, I've specified my server, my username, and my password. I'll save this file and go back to the test file and open that one and you see I got a blank page and I switch over here and my email is already in my Gmail. So while it was possible in ColdFusion 8 to wrap up your data source and SMTP server settings in the application variables, you still had to be cognizant of each and every query and <cfmail> tag used to make sure they were used in the correct settings.
In ColdFusion 9, you can just set them in your Application.cfc and only think about them when you need to step outside of the normal operating procedures.
- Using new implicit Getters and Setters in ColdFusion Builder
- Utilizing RAM disks using VFS
- Managing the system cache to boost site/application performance
- Learning the new ternary and chained operations
- Loading detailed Google maps with markers and notations onto a web page