330
Chapter 15: Using Persistent Data and Locking
Accessing and changing session variables
You use the same syntax to access a session variable as for other types of variables. However, you
must lock any code that accesses or changes session variables.
For example, to display the number of items in a user’s shopping cart, use favorite color that has
been set for a specific user, for example, use the following code:
<cflock timeout=20 scope="Session" type="Exclusive">
<cfoutput>
Your shopping cart has #Session.ShoppingCartItems# items.
</cfoutput>
</cflock>
To change increase the number of items in the shopping cart, use the following code:
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.ShoppingCartItems = Session.ShoppingCar 1>
</cflock>
Ending a session
If you use J2EE session management, the session and all session variables are deleted when the
user closes the browser. If you use ColdFusion session management and do not explicitly
terminate a session, for example when a user logs out, the session variables remain in ColdFusion
server memory until the session time-out period elapses.
To explicitly end a session, you can delete the session variables, or you can clear the session scope,
as follows:
<cfset StructClear(Session)>
If you use J2EE session management, you can also invalidate the session, as follows:
<cfset getPageContext().getSession().invalidate()>
This line creates a pointer to the servlet page context and calls an internal method to reset the
session.
Configuring and using application variables
Application variables are available to all pages within an application, that is, pages that have the
same application name. Because application variables are persistent, you easily can pass values
between pages. You can use application variables for information including the application name,
background color, data source names, or contact information.
You set the application name in the
cfapplication
tag, normally on your application’s
Application.cfm page. The application name is stored in the
Application.applicationName
variable.
Unlike client and session variables, application variables do not require that a client name (client
ID) be associated with them. They are available to any clients that use pages in the application.
Caution:
To preserve data integrity, put code that uses application variables inside
cflock
tags. For
information on using
cflock
tags see
“Locking code with cflock” on page 333
.
The following sections describe how to configure and use application variables.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......