Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Application Variables vs Request Variables

Explorer ,
Oct 08, 2008 Oct 08, 2008
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,
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 2008
> 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?
>

Must be, because I use application variable all the time in my CF8 CFML
code and have been since I started using ColdFusion with version 4.5.

But since I do not know what you are doing, I can say much about *why*
you are having difficulty using application variables in your code.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 2008
> 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
>
> Alternatively #REQUEST.varname#

Some "confusion" arose on this sort of topic in old versions of CF (prior
to 6) in that they were very buggy when it came to doing even simple
operations with shared-scope variables (server, application, session). The
solution to this was to <cflock> every single access to this sort of
variable. What a pain in the bum that was. As such, people shunned them -
and adviseably so - for things like DSNs, because it would mean either
locking the entire query (bad), or copying the application-scoped DSN into
some other request-specific scope (most often the request scope).

However that's been fixed since CFMX6.

Variables that are global to the application should be put in the
application scope. Variables that are global to a request should be put in
the request scope. Almost always, a DSN is going to be global to the
application, so application.dsn is the better fit there.


> -- but I dont seem able to use #APPLICATION.varname# in my pages
> (cf8). Am I doing something wrong?

Quite possibly. But how do you expect us to answer that sensibly without
you showing us the relevant code? Guess? I therefore guess that - yes -
you are definitely doing something wrong ;-)


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

Correct. But the specifics of this DSN thing aside, a bigger concern is
getting this application scope working. Because it should (work).

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 2008
<cfcomponent output="false">


<cfscript>
//the application name (should be unique)
THIS.name = "olo";
//how long the application variables persist
THIS.applicationTimeout = createTimeSpan(0,2,0,0);
//define whether client variables are enabled
THIS.clientManagement = true;
//where should we store them, if enabled?
THIS.clientStorage = "cfClientVariables"; //cookie||registry||datasource
//define where cflogin information should persist
THIS.loginStorage = "session"; //cookie||session
//define whether session variables are enabled
THIS.sessionManagement = true;
//how long the session variables persist?
THIS.sessionTimeout = createTimeSpan(0,0,20,0);
//define whether to set cookies on the browser?
THIS.setClientCookies = true;
//should cookies be domain specific
//i.e. *.domain.com or www.domain.com
THIS.setDomainCookies = false;
//should we try to block cross-site scripting?
THIS.scriptProtect = false;
//should we secure our JSON calls?
THIS.secureJSON = false;
//use a prefix in front of JSON strings?
THIS.secureJSONPrefix = "";
//used to help ColdFusion work with missing files
//and directory indexes. tells ColdFusion not to call
//onMissingTemplate method.
THIS.welcomeFileList = "";
//define custom coldfusion mappings.
//Keys are mapping names, values are full paths
THIS.mappings = structNew();
//define a list of custom tag paths.
THIS.customtagpaths = "";
</cfscript>


<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset APPLICATION.companyName = "MyCompany">

<cfreturn true />
</cffunction>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 2008
<body>

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

</body>
</html>

Throws an error. Must I restart the cf server each time I make a change to Application.cfc? (and if so is there an easy way to restart the cf server?)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 2008
The only application variable I see is companyname. Are you able to access it in any pages in that application?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 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 . .. . .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 2008
is there a setting in the cf administrator that could be doing this?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2008 Oct 08, 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2008 Oct 08, 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?




Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 14, 2008 Oct 14, 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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2008 Oct 09, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2008 Oct 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2008 Oct 09, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2008 Oct 09, 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2008 Oct 09, 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.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 10, 2008 Oct 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2008 Oct 10, 2008
@Fober1,
ProjectedSurplus' cfscript code does what you suggest.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2008 Oct 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 10, 2008 Oct 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2008 Oct 14, 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().



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources