views:

353

answers:

1

I have to use a Variable(Query Resultset) in ColdFusion, which will get the results from Other Application DB, and stores in Coldfusion Application.

The main idea is that I need to call the other Application DB only at Server startup time and cache the results in local. And I need to read the variable in other pages in my Application. I won't overwrite that variable in any page.

On googling I found that 'onApplicationStart' is useful to assign the variables at Application Startup time.

Is using the onApplicationStart fine or is there any other way? We can assign a variable at startup time(one time).

If onApplicationStart is fine: how to use? Maybe any link where it is explained clearly is helpful.

+7  A: 

Well, it depends. How often will this query data be updated? If it really is unchanging, then onApplicationStart() is a fine place to put it. However, if it will change every so often, you can just tell Coldfusion to cache the query for a certain period of time, then you don't need to mess with onApplicationStart(), but rather when you call the query it will return the cached result automatically (within your specified time period).

Regardless, I would write a custom function to retrieve the data. Then it will be trivial to call it from onApplicationStart() or elsewhere.

Startup.cfc: (Named whatever you like)

<!--- Replace the datasource name with your db name --->
<cffunction name="getStartupQuery" hint="Returns a query recordset for startup">
    <cfargument name="datasource" required="no" type="string" default="OtherAppDB">
    <!--- Init the query variable --->
    <cfset var result = queryNew("id")>

    <!-- Get the query dataset --->
    <cfquery name="result" datasource="#arguments.datasource#">
         YOUR QUERY HERE
    </cfquery>

    <cfreturn result>
</cffunction>

Application.cfc: (Just the important parts)

<cffunction name="onApplicationStart">
    <!--- init the startup.cfc, then retrieve the data
    and save it to the application scope. Remember the component name must match
    your component above --->
    <cfset var startup = createObject("component", "startup")>
    <cfset application.varFromOtherDB = startup.getStartupQuery()>
    <cfreturn true>
</cffunction>

Now, you should be able to access this variable from any CFM or CFC in your application using:

<cfset myNewVar = application.varFromOtherDB>
or
#application.varFromOtherDB#

IF you use the onApplicationStart() method, I highly recommend implementing a method to reinit the application. For an example, see this other discussion.

Dan Sorensen
Hey Dan, That is very nice.I am sure that I don't need to reinitialize this Query apart from Application Startup, as the Query values from other DB are changed rarely. So I don't want to call it every time.But one Surprising thing is, in My Application we dont have "Application.cfc"(I think they recently upgraded to CF8 from CF7, so not implemented it).But we are using Application.cfm files to set the variables common to all pages.Do you think the above code in Appliaction.cfc we can use in Application.cfm(which is included in all the pages) or need to create new Application.cfc?
CFUser
Are you using application.cfm and the <cfapplication> tag? I'm not experienced with the application.cfm method, so I'll defer to others to answer that question. Much of the code will remain the same. You may need to research the appropriate way to set application scope vars with application.cfm:http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_16.html
Dan Sorensen
From http://livedocs.adobe.com/coldfusion/8/htmldocs/AppEvents_01.html , "If your application has an Application.cfc, and an Application.cfm or onRequestend.cfm page, ColdFusion ignores the CFM pages." I believe it is better practice to use one or the other. The onRequest method will give you the same capability to run scripts before a cfm page is actually executed.
Eddie
I agree with Eddie that you should use one or the other. I would check to see if you are using application.cfm before I'd resort to onRequest() though. If you're not using application.cfm, then setting up an application.cfc would be easy. It seems that's running more often than you really need.
Dan Sorensen
Yes, I am using application.cfm and the <cfapplication> tag in ApplicationSettings.cfm file, but not using application.cfc.in application.cfm I am including ApplicationSettings.cfm file top
CFUser
Well, then, you can't use onApplicationStart. That's an Application.cfc method.
Al Everett
Yes, I tried with onApplicationStart, it didn't work, But I followed the procedure to setup Application variables in http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_16.html which Dan mentioned, Its working perfect, thanks
CFUser