Skip to main content
Participating Frequently
October 29, 2009
Answered

Server not maintaining sessions?

  • October 29, 2009
  • 1 reply
  • 919 views

I'm using the ColdFusion builder to develop an application on my local machine. I installed the built in server that comes with ColdFusion 9's developer edition.

When I try to use the adobe provided code to set a session variable:

<cflock timeout=20 scope="Session" type="Exclusive">

    <cfset Session.ShoppingCartItems = 0>

</cflock>

and then access it like so:

<cflock timeout=20 scope="Session" type="Exclusive">

    <cfoutput>

        Your shopping cart has #Session.ShoppingCartItems# items.

    </cfoutput>

</cflock>

I get an error that says that session variable does no exist. My application.cfc file has these two properties defined:

<!--- Should we even use sessions? --->

<cfset this.sessionManagement = true>

<!--- How long do session vars persist? --->

<cfset this.sessionTimeout = createTimeSpan(0,0,20,0)>

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Those session variables change. The cfid increments by one when transferring between pages and the cftoken is also changing.

    Could this be caused by another application.cfc setting?


    Yeah, I'm pretty sure you also need to this.setClientCookies=true for the sessions to actually stick.

    --

    Adam

    1 reply

    Inspiring
    October 29, 2009

    Firstly, you do not need to use <cflock> in either of those situations you are using them.  If the docs still say that's recommended, the docs need to be changed.

    Secondly, how do those two code snippets relate to each other?  Are you sure the first bit of code - that sets the variable - executes before the second bit?

    --

    Adam

    Participating Frequently
    October 29, 2009

    The Coldfusion 9 docs recommend locking when setting session variables. Can you explain why this isn't necessary?

    The first snippet of code (the set) is on one page that links to a second page that contains the retrieve/get call. Both pages are in the same directory and thus have the same application.cfc file. In testing, I was going to the first page to set the session variable, and then clicking the link to the second page with the get call.

    Inspiring
    October 29, 2009

    The Coldfusion 9 docs recommend locking when setting session variables. Can you explain why this isn't necessary?

    I suspect it's because those specific docs haven't been updated since CF5, which this was a requirement.  CF5 (and earlier) used to have massive problems with shared-scope variables, necessitating single-threading all access to them when writes were happening.  This required an EXCLUSIVE lock around all writes, and a READONLY lock around reads.  If the docs say you need an exclusive lock around a read, then that's just plain wrong (for any version of CF).

    Since CF was ported to Java, these considerations are no longer relevant.

    The first snippet of code (the set) is on one page that links to a second page that contains the retrieve/get call. Both pages are in the same directory and thus have the same application.cfc file. In testing, I was going to the first page to set the session variable, and then clicking the link to the second page with the get call.

    Can you check that your CFID and CFTOKEN values are staying the same between requests?

    --

    Adam