Skip to main content
Inspiring
October 8, 2008
Question

Application Variables vs Request Variables

  • October 8, 2008
  • 24 replies
  • 1875 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

    Inspiring
    October 9, 2008
    > > Must I restart the cf server each time I make a change
    > > to Application.cfc?

    >
    > I would disagree with the others, and say no. You don't have to.

    OK, well lets put this in perspective. We (Azadi, myself) are talking
    *specifically* about the OP's code which is in onApplicationStart(). We
    did not blanketly say "yes you do", we both said "for code changes *in
    onApplicationStart()*".

    As with any other function, one can make any changes to
    onApplicationStart() as one likes, and the next time it gets called, the
    changes will be reflected.

    However given the function is only called *when the application starts*
    (hence the name), it's all a bit moot until the application starts again.

    I suppose the OP could wait for the application to time out (possibly
    changing the time out to be very short in CF Administrator first, so one
    doesn't have to wait - the default - two days before the thing times out),
    but the more expedient approach would be to restart CF.

    To answer the OP's question that I didn't spot initially: the best (?) way
    to do this is to just cycle the service.


    > I think it is
    > sufficient to open a CFM page, for the application to automatically switch over
    > to the new Application.cfc settings.

    It depends what you mean by "the new Application.cfc settings". I'm sure
    the CFC does recompile, like any other CFC would the next time it's called,
    but that's irrelevant to the situation at hand because there's a difference
    between "recompile" and "the code in question actually running". TheOP
    needs the code to run, not just for it to be in a state ready to run.


    > You could be having the error because the application is
    > not configured to use application variables.

    Good point.

    --
    Adam
    BKBK
    Community Expert
    Community Expert
    October 9, 2008
    ProjectedSurplus wrote:
    > Must I restart the cf server each time I make a change
    > to Application.cfc?


    I would disagree with the others, and say no. You don't have to. I think it is sufficient to open a CFM page, for the application to automatically switch over to the new Application.cfc settings.

    But you might need to restart the server if you changed the application name. It is possible that an application that bears the old name will still be active in the background. There is no need for Coldfusion to be reserving memory for it. So, you would restart the server to kill it. However, even here, a restart is recommended, not compulsory.

    Now, on to your question proper. At the beginning, that is, in the so-called pseudo-constructor, I would define only the system attributes(name, applicationTimeout, etc). I would move the non-system attributes mappings and customtagpaths to onApplicationStart, and redefine them as

    <cfscript>
    //define custom coldfusion mappings.
    //Keys are mapping names, values are full paths
    application.mappings = structNew();
    //define a list of custom tag paths.
    application.customtagpaths = "";
    </cfscript>

    Another point is that the value of THIS.scriptProtect should be "none", not false.

    > 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?


    No, you're right. You would usually have to set such a datasource name in onApplicationStart().

    You could be having the error because the application is not configured to use application variables. To verify this, navigate to the page Server Settings => Memory variables in the Coldfusion Administrator. Make sure that the boxes Enable Application Variables and Enable Session Variables are checked. Don't forget to press the button to Submit Changes. Oh, and, what error did you get?




    Inspiring
    October 15, 2008
    quote:

    Originally posted by: BKBK
    At the beginning, that is, in the so-called pseudo-constructor, I would define only the system attributes(name, applicationTimeout, etc). I would move the non-system attributes mappings and customtagpaths to onApplicationStart, and redefine them as

    <cfscript>
    //define custom coldfusion mappings.
    //Keys are mapping names, values are full paths
    application.mappings = structNew();
    //define a list of custom tag paths.
    application.customtagpaths = "";
    </cfscript>




    Why would you do this? (btw I know NOTHING about these so am not questioning, just trying to gain insight)

    Inspiring
    October 9, 2008
    in your script you set:
    THIS.clientStorage = "cfClientVariables";

    do you actually have that db and datasource set up??? if not, just
    change it to "cookie".

    and, as Adam has already mentioned, you HAVE TO restart your aplication
    every time you make change in the initialization script or in
    onApplicationStart() method.

    in order to avoid this, what i do is add the following in
    onRequestStart() method:

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

    then i can restart my app by adding ?appreset=something_only_i_know to
    any url.

    note: obviously, 'something_only_i_know' is just a placeholder for some
    un-geussable string.

    hth


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    October 8, 2008
    Something is definitely screwy in the script section above.

    Using the Application.cfc template from here ( http://www.coldfusionjedi.com/index.cfm/2007/11/5/ApplicationCFC-Template-Update) provides no problems to using APPLICATION.____ variables.

    Is there a "better" Application.cfc template I/we newbies can work from?
    Inspiring
    October 8, 2008
    On a brand new site when I took out the <cfscript> section of the Application.cfc above I could use

    <cfdump var="#APPLICATION.companyName#">

    Any idea why?
    Inspiring
    October 8, 2008
    is there a setting in the cf administrator that could be doing this?
    Inspiring
    October 8, 2008
    > Throws an error. Must I restart the cf server each time I make a change to Application.cfc?

    Changing the bit of it that only runs when *the application first starts*?
    Yes.

    I ran a test rig with your Application.cfc, and it works fine.

    --
    Adam
    Inspiring
    October 8, 2008
    It could be a file location thing. Is the template where you are attempting to access the application variable in the same directory as the application.cfc file or a sub-directory of it?
    Inspiring
    October 8, 2008
    NO, I cannot access that (or any APPLICATION.____ that I have similarly created) variables in my pages even after restarting the cf application server.

    I've tried setting up an entirely new website and everything . .. . .

    Inspiring
    October 8, 2008
    The only application variable I see is companyname. Are you able to access it in any pages in that application?