Question

Application and Session Variables and Locking

Asked by: queet

I 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#</cfoutput>)

Code for instanting cfc and storing it into application variable:
if(not isdefined("application.objSystem")){
  application.objSystem = createobject("component","System");
}
 
Code for calling method inside CFC application variable:
if(application.objSystem.listValuesFound(session.usergroup, "Instructor,System_Administrator")){
				menu = menu & '<li><a href="ViewCourses.cfm?#session.URLToken#">Create, View or Edit Courses</a></li> <br /><br /> #chr(10)##chr(13)#';
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-09-25 at 12:08:53ID23763804
Tags

Coldfusion MX 7

,

500 Null Null

Topics

ColdFusion Application Server

,

ColdFusion Studio

,

Cold Fusion Markup Language

Participating Experts
2
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. cfoutput using session variable works but not in query
    this is my code: <cfoutput>#session.select#</cfoutput> i'm using this to output all form.select: <b>Regions:</b>&nbsp;<cfoutput>#session.select#</cfoutput> which outputs like: Regions: PANJ, VAMDDC, MA i'd like to be able to do th...
  2. Return Multiple Variables from a CFC?
    I would like to invoke a component and have it return TWO values, which can be used as variables on the page. I know I can do this by setting session variables. However, is there any other way to do it? For example, I know that this is wrong, but it will sort of explain wh...
  3. CFC lifespan
    Hi, I'm a little confused with the lifespan of a CFC. If I create an instance of one on a page, is it removed when I change pages or does is stay persistent until I delete it. I'm trying to work out what is the best practice for develop a modular chunk of code to create / u...
  4. Invoking a cfc using cfset
    Experts, Can you explain invoking a cfc using the cfset tag instead of using the cfinvoke method. Why would you use this method instead of cfinvoke? For example: -cfset request.presentations.presentationManager.initializeParticipantSession(cookie.cfid, form.name, form.email...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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.

What I do for restarting a application is add this script in my onrequeststart method

<cfscript>

    if(isdefined('url.restartapp')){
         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

 

by: zadocPosted on 2008-09-27 at 19:50:52ID: 22589537

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

 

by: gdemariaPosted on 2008-09-27 at 20:18:53ID: 22589601

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.

 

by: queetPosted on 2008-10-01 at 07:26:42ID: 22614623

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.listValuesFound(....)) in my pages?  In my application.cfm file, I am copying the session variables to the request scope (ex. request.userinfo = session.userinfo).  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 (ex. session.userinfo.lastname = "BB" or request.userinfo.lastname="EE")?

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.

 

by: queetPosted on 2008-10-01 at 07:29:31ID: 22614656

BTW, the Null error has been resolved.

 

by: gdemariaPosted on 2008-10-01 at 10:12:09ID: 22616610

> 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.listValuesFound(....)) in my pages?

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.

 

by: gdemariaPosted on 2008-10-01 at 10:12:28ID: 22616614

> BTW, the Null error has been resolved.
cool! how?

 

by: queetPosted on 2008-10-07 at 08:13:39ID: 22660155

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

 

by: gdemariaPosted on 2008-10-07 at 08:24:51ID: 22660258

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



 

by: queetPosted on 2008-10-08 at 10:21:15ID: 22671037

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.lastname#</cfoutput> displays Doe)

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.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...