Skip to main content
Inspiring
October 8, 2008
Question

Application Variables vs Request Variables

  • October 8, 2008
  • 24 replies
  • 1874 views
I've read a number of forums and cf books but they seem to contradict each other.

To me it seems better to put say the datasourcename into the application variable -- but I dont seem able to use #APPLICATION.varname# in my pages (cf8). Am I doing something wrong?

Alternatively #REQUEST.varname# I can use no problem but this to me is requiring the cf server to do unnecessary extra work (ie before each page request) is it not?

Thanks in advance,
    This topic has been closed for replies.

    24 replies

    BKBK
    Community Expert
    Community Expert
    October 16, 2008
    ProjectedSurplus wrote:
    Similarly though, at the moment I am defining an APPLICATION.imageSource = " http://70.69.etc " with the thought to store my images on a separate server later on (maybe Amazon S3 or the like). Is this the sort of thing better done via an application.mapping ?

    If the imageSource URL is a constant throughout the application, then it's a good idea to set APPLICATION.imageSource = " http://70.69.etc " in onApplicationStart. I expect the struct, application.mappings, to have keys whose values correspond to the mappings you defined in the Coldfusion Administrator.



    BKBK
    Community Expert
    Community Expert
    October 15, 2008
    Suppose you define a variable this.myVar in the pseudo-constructor of Application.cfc. Then it will be a public, instance variable of Application.cfc, and of that component alone.

    However, it is obvious that you wish the variables mappings and customtagpaths to be available to every component throughout the application. So, you put them in application scope. Assuming you'll never need to change them once the application starts, the place to define them is in onApplicationStart().



    Inspiring
    October 15, 2008
    Thanks, I have not gotten to needing or even reading up on either mappings (I assume are roadmaps to directories) or customtags but what you say makes sense so I will follow (with gratitude) your suggestions (undoubtedly saving myself hours at some future date).

    Similarly though, at the moment I am defining an APPLICATION.imageSource = " http://70.69.etc " with the thought to store my images on a separate server later on (maybe Amazon S3 or the like). Is this the sort of thing better done via an application.mapping ?
    Inspiring
    October 10, 2008
    > On which note, thanks all for
    > the comments, I've retained more from your "debates" than from extensive
    > reading.

    Well at least it serves *some* positive purpose then ;-)


    > Still if there is a template Application.cfc that anyone can
    > recommend it would be IMHO a great addition to this thread.

    The docs! Not to be too pointed, but *RTFM*. It's all in there. Go to
    livedocs and search for Application.cfc in the index. That's all I did,
    before participating in this conversation.

    --
    Adam
    Inspiring
    October 10, 2008
    For whatever it is worth, I found the Application.cfm file sometimes updated when I saved changes to it but not always (almost certainly had something to do with the Application name = value which I played with but did not figure out).

    Obviously though the restart worked if it was nonetheless unwieldy even on my devServer situation (so the

    <!--- restart application --->
    <cfif structkeyexists(url, "appreset") AND url.appreset eq
    "something_only_i_know">
    <cfscript>
    onApplicationStart();
    </cfscript>
    </cfif>

    was welcomed even if I have not used it yet). On which note, thanks all for the comments, I've retained more from your "debates" than from extensive reading. Still if there is a template Application.cfc that anyone can recommend it would be IMHO a great addition to this thread.
    BKBK
    Community Expert
    Community Expert
    October 10, 2008
    @Fober1,
    ProjectedSurplus' cfscript code does what you suggest.

    Inspiring
    October 10, 2008
    Hi,

    Do you have an cfapplication tag in your application.cfm file?
    This tag is necessary (not sure about CF8) to get application and session variables working.

    <cfapplication
    name= "MyApplication"
    sessionmanagement= "Yes"
    sessiontimeout= "#CreateTimeSpan(1,0,0,0)#"
    applicationtimeout= "#CreateTimeSpan(1,0,0,0)#"
    >
    cheers,
    fober
    BKBK
    Community Expert
    Community Expert
    October 10, 2008
    @Adam Cameron
    I get the feeling you missed my point.Your argument is restricted to your corner of the woods.

    Hosting-server admins will tell you that "27 changes to a file on a production box (let alone the same CF instance!) " is quite common. Yes, on production servers. It's a jungle out there. There are all sorts.

    Restarting the server is, in general, a major decision. That tells me that there are very likely ways to rerun Application.cfc without having to restart the server.

    Azadi has given one fully worked-out example. Another possibility is for ProjectedSurplus to temporarily change the application's name to ono2. That would kick-start the whole application. Ono's applicationTimeout was 2 hours. ProjectedSurplus could then carry on with the name ono2 and hope that after about, say, 3 hours any remnants of ono would have been wiped out from Coldfusion's memory. He could then change the name back to ono.

    There might be other ways, like your suggestion of doing something with the applicationTimeout. But I've never tried that.


    Inspiring
    October 9, 2008
    Adam Cameron wrote:

    > Actually, here's a thought. Is it possible to simply *call* the
    > onApplicationStart() method, if one needs to re-run it?

    yep, it's possible - check out my previous post for an example.



    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    October 9, 2008
    > There are no perspectives about it, I'm afraid. Jack may decide to change his
    > Application.cfc 27 times in a quarter of an hour and Jill's site may have 2300
    > paying clients logged in

    How likely do you *really* think it would be that "Jack" is going to have a
    "mess around" site *on the same CF instance* as "Jill's" busy production
    site? You are really stretching credibility with your squirming attempts
    at going "but I'm right! Honest I am!".

    This is a completely specious strawman argument. And why did I know you'd
    be showing up with one, at some stage this evening? Oh well.


    The guy is testing his code and wanting to know the best way of getting
    results. He can sit there all day wondering WTF his onApplicationStart()
    modifications don't work, or he could just cycle his CF service.

    He's not on a production server here.

    Speaking of production servers... I would expect to get in an awful lot of
    trouble with my line manager if I was needing to make 27 changes to a file
    on a production box (let alone the same CF instance!) that is shared with
    another site which might have 2300 concurrent visitors on it. That's just
    not how things are done.

    Not least of all because a production server is no place to be testing
    code, whatever the circumstances.

    Actually, in a situation like that, the environment would be clustered and
    have some fault tolerance applied, so it would actually be fairly safe to
    cycle the services.

    But, yes, if it makes you feel better then you're dead right that one
    should not be either testing code on a busy production server, in such a
    way that would require CF service restarts.

    There. You are right.


    > In general, the decision to restart the server is too heavy to compare with
    > that of restarting an application.

    How does one do that? Restart an application that is. Just set the
    application timeout to be very low, and wait? It's not something I need to
    do very often, so I have to say I dunno.

    Actually, here's a thought. Is it possible to simply *call* the
    onApplicationStart() method, if one needs to re-run it? Exposing my
    shortcomings some more, I work mostly on some legacy software which was
    written before the days of Application.cfc, so I've not had opportunity to
    get up to speed with its vagaries on a daily basis yet. Indeed the only
    investigation I've done is to answer questions here, actually.

    --
    Adam
    BKBK
    Community Expert
    Community Expert
    October 9, 2008
    There are no perspectives about it, I'm afraid. Jack may decide to change his Application.cfc 27 times in a quarter of an hour and Jill's site may have 2300 paying clients logged in -- and Jack and Jill may both be on the same server! In general, the decision to restart the server is too heavy to compare with that of restarting an application.