Ask a Jedi: Dealing with session variables and multiple windows

This post is more than 2 years old.

Michael asks an interesting question about a conflict he is having with multiple windows and session data:

I have an application that uses the session scope to store a single record in a table for editing. it all works great until some user forgets he has a window buried and starts editing another of the same kind of record. if it was a contact record he would then be using the session scope to store two contacts and that doesn't work because the variables would all have the same name (i.e. session.contactInstance.FirstName) so when the user saves the two records, one of them gets over written with values from the other record and you end up with two of one and none of the other. So how to keep them separate? is there an easy way to dynamically name the variables? How is this problem normally handled?

First off - let me address the simpler part of your question, and that is about dynamically naming the variables. Since the Session scope is a structure like everything else in ColdFusion, that makes dynamically storing data very easy. Instead of session.contactInstance having your data, you could easily have session.contactInstances exist as a structure, and the primary key of each contact would point to a structure of data. So if I'm editing user ID number 6, I'd have session.contactInstances[6].firstName.

But that isn't really what you are concerned about I think. The issue you are having is with your edit process in general. You didn't say it - but are you using a multi-step form? That's typically the only time you would bother storing the data in the session scope. If it is a one page form, then you shouldn't be storing it in the session scope at all.

If you are indeed using a multi-step form, you could simply update your code to notice if a record was being edited and never finished. If the user created a new window and tried to edit user 8, you could throw an error and tell the user to finish editing user 6, or simply clear out all the data you had stored before. In other words - ensure that the first step of your multi-step form performs a 'clear' of any and all data that may have been there before.

If you do want to support multiple windows, then you can use what I suggested above about a struct of structs using the primary key of the data.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Dave Ferguson posted on 2/6/2008 at 9:08 PM

Great points Ray. One other thing to add when dealing with session vars. When you are done with the session var delete it. Don't keep session vars around if you have no intention of using them again.


Comment 2 by Michael White posted on 2/7/2008 at 3:59 AM

I like the dynamically naming the Struct solution, I'll play around with that. It's Sort of a multi-step form in that the user enters information on a form, there's a processing page to validate and otherwise manipulate the data, then there's another page that logs and sends notifications. I learned that process from the CFWACK books and now I have to unlearn it to make better use of AJAX events and components.
I always delete my session variables at the end of that process but sometimes they don't finish the process. A couple of my users take multi-tasking beyond their limits.

Comment 3 by Chris Phillips posted on 2/9/2008 at 6:07 AM


I bet a lot of people forget to get rid of session variables when they are done using them.

Below is an example of the end of a process where I call a save method on an Active Record style business object and then get rid of it. Hope the code comes through...

<!--- Save Project --->
<cfset />

<!--- Get rid of project bean from Session --->
<cfset structDelete(session.edit,'Project') />

Comment 4 by Joshua Curtiss posted on 2/14/2008 at 11:55 AM

I got burned with the session scope thing for editing multipage forms way back in CFMX 6 when CFCs were new and more expensive (in a system resources sense). To be "smart", I thought I'd put the CFCs in the session scope to reduce object re-creation. Well, it worked.. but then I had that issue with multiple open windows stomping on each other. Ugh, what a nightmare way to learn that lesson.