when I say you do not need to lock these methods.. what i mean is... normal locking applies. some you may, some you may not...
Main Topics
Browse All TopicsI have four CFCs that I would like to be available to all pages. Initially, I was storing the CFCs in a session variable, but one of the exchange experts recommended that I store the cfcs in an application variable, partly to deal with my JRun (out of memory) situation. I have made the changes, but now I am receiving a 500 null null error. While trying to search for solutions on resolving the 500 null null error. I came across a couple of posts that say you should lock application and session variables. I sort of understand the logic behind the locking, but I am not really clear whether it is an absolute must and when and how to use them. The bloggers even said you should lock application/session variables even during reads. I was locking my session variables but then I notice a drop in the performance of my application so I removed them. So, I have the following questions:
1. How do you store a CFC in an application and reference (and or call) the methods stored in the CFC? I think I am doing something wrong which is why I am receiving the 500 null null error message. Please see code snippet to see my attempts.
2. If I need to modify a method in the CFC or add any new methods, will the changes be reflected in the application variable since the state of the lasts for as long as the application or would I need to restart the coldfusion server for the cfc to be updated with the new changes?
3. Do I need to lock all session and application variables even when calling the CFC application variable to invoke (call) a function and even when passing session variables to a CFC function as arguments or querying the database? Also what about outputting information stored in a session variable (ex. <cfoutput>Logged in #session.user_firstname# #session.user_lastname#</c
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Zadoc is correct assuming you are using application.cfc file. If you are using the application.cfm file, then there is no onApplicationStart function and you would have to test for the existance of the application variable.
Application Scope - I always set these only in the application.cfc/.cfm file. Their use is for creating an all-user variable or instantiating a cfc. A good use of an application function is the application.datasource which is read-only and never changes, but available to all users. You can test for these and create them within cflock if using application.cfm. If using application.cfc, as Zadoc says, neither lock or test are necessary. Application.cfc is a great way to go.
Session Scope - Good example of use for these is to sustain login information. I think these variables are often grossly over-used as developers use them almost as "local" variables to just hold any value - IMHO that's not good at all as it consumes memory and leads to bugs and difficulty debugging. Set these variables where needed (during login for example). Check for their existance and set them, both within a CFLOCK, including reads. My preferred method is to copy all my session variables into request scope variables in the application.cfm/.cfc file. Then just read from the globally available request scope variable without concern about locking throughout the app.
I am using Application.cfm because I am more familiar with it than Application.cfc. In the future, I am thinking about using Application.cfc. When using Application.cfc, does it automatically lock session variables as well?
Do I need to lock the application variable when using the application variable to call a function (ex. application.objSystem.list
I have never used locks when reading or setting session variables in any of my applications. I just recently discovered that locking is required on session variables. Plus, my colleagues and I took a Coldfusion training course and the certified coldfusion instructor never mentioned anything about locking session variables. I am a little confused about why it is required to lock session variables because I thought that they are suppose to be unique each user.
> When using Application.cfc, does it automatically lock session variables as well?
You don't need to lock application/session variables within the onApplicationStart and onSessionStart functions used inthe Application.cfc file. So, the answer is Yes, sort-of, because it's not necessary there. You would need to lock them in other places outside the app.cfc file..
> Do I need to lock the application variable when using the application variable to call a function (ex. application.objSystem.list
No, you don't need to lock application scope variables to use the components
> However, if I change let's say the last name in the session variable or the request object, does the other one pick up the change or do I have to reset them equal to each other
No, there's nothing automated. You are physically copying them and would need to keep them in sync. To me, however, this is easy because my session variables are used only to maintain login and session states. If the person changes their login profile, you simply call the function that reloads the request variables from the tables... I never use session variables as local variables and change them all the time.
> I am a little confused about why it is required to lock session variables because I thought that they are suppose to be unique each user.
I agree it's confusing. I once read CF MX7 & 8 didn't need as much locking, but then I read it somewhere else it did. I have been trying to find confirmation, but its hard to find someone to really say it definitively and explain under what circumstances.
The null error was resolved by changing some permissions on a file that coldfusion was trying to access and by restarting the server.
You said that I would need to keep the request and session objects in sync. Can this be achieved if I set request.user = session.user in the Application.cfm file and only make changes to the session object variables whenever the variables need to be modified? Or, do I still need to copy the session object back to the request object everytime I make a change to one of the variables?
As far as locking, is this the proper way to lock a session variable (see code below) and is there a way to lock a session variable within a cfscript tag?
<cflock timeout="5" throwontimeout="yes" type="EXCLUSIVE" scope="SESSION">
<cfset session.message = "....">
</cflock
> Do I still need to copy the session object back to the request object everytime I make a change to one of the variables..
They are completely separate variables, so if you change one and need the other to be in sync, then you must change both. There is no automation.
But if you need to sync these, then ask yourself if you should be using a session variable for whatever it is... I don't run into this because I think it's an overuse of the session variable. For example, your session variable keeps your user's ID, that means he's logged in. Then, in application.cfm file, you load request variables from the user's table to keep his Name and Email handy for that request. No need to use session variables for those. If the user modifies his email, you update the table and reload the request variables.. no synchronization needed and you're not constantly using the session variables for unnecessary things.
IMHO, if it's not maintaining the session, then you don't need the session variable.
Use type="EXCLUSIVE" when writing the session and use SHARED when reading the session variable.
So, what is session.message used for? Why does it need to be session?
II'm am using session.message to store error or alert messages.
I use request variables in the application.cfm to store information that is not user specific but needed by all pages like the datasource name, path to stylesheets, support contact information, etc.
I use session variables to store information that is user specific and that needs to be persistent (available to other pages) like the user's information (lastname, firstname, id, permissions, etc.) and form information that needs to be accessible to other pages or persist in memory. For example, if a form doesn't pass validation, I need to redirect the user back to the form and display the error message. Additionally, the form needs to be prefilled with the values that the user chose or entered prior to the validation failure and redirect. Session variables allows me to perform these actions: prefill form and display error messages.
In another post, someone recommended setting request.variablename = session.variablename in the application.cfm file so that you don't have to lock the entire form to refer to the session variables. I assummed that request.variablename = session.variablename was passing a reference to the session variable and not actually copying. I believe that is how Java handles objects that are set equal to each other. In that case, changes are reflected automatically. On a couple of pages, I changed information that was stored in some of the session variables and it appeared that the request object picked up on those changes (ex. if change session.form.lastname = "Doe" >> <cfoutput>#request.form.last
Perhaps, I'm not understanding variable scopes but I don't understand why you would store user information in the request object when request object only lasts for that particular request. I understand that by storing it in the application.cfm you making it persistent but wouldn't that make it available to all users? I thought application variables are meant to be available to all pages and all users. In Ben Forta's MX7 Construction Kit (Chapter 21, page 628), he stores user information in the session object.
It almost seems like sessions are now a bad thing and it is really not clear whether locking is mandatory or optional based on various posts. Additionally, I am not clear on when and how to use them. I don't want to overuse them and I don't want to be using them if it is not necessary.
Business Accounts
Answer for Membership
by: zadocPosted on 2008-09-27 at 19:48:33ID: 22589533
You dont need to check if application.objSystem exists. The only time it wont exist is if the application ends... and when it ends, onapplicationstart instantiates.
pp')){
What I do for restarting a application is add this script in my onrequeststart method
<cfscript>
if(isdefined('url.restarta
onApplicationStart();
};
</cfscript>
so... if I want to restart the application I just add ?restartapp at the end of the url string.
no you do not neet to lock when calling on these methods.
I usually instantiate all of my cfcs in onapplicationstart
As for the null null error... What type of database are you using?
waiting for your response