Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe ColdFusion

  • Print
  • + Share This
This chapter is from the book

Distributed CFCs, Serialization, and ColdFusion 8

The previous chapter discussed storage of CFCs in the session scope. One problem with storing CFCs in this way is that when more than one server is in use, each server has its own session variables; if a user moves to another server during the course of a visit to your site, the session data can be lost. One way to solve this problem is to create "sticky" sessions, which requires special software or hardware—but what if the Web server goes down?

Many Java application servers that ColdFusion runs on can support distributed sessions. Basically, what the servers do is "serialize" the data in the session scope, which means that a server writes the data in a flat form (turns it into a string, in the same way that CFWDDX does) and passes it to all other servers in the cluster that might process the new request. No matter which server is used, it has a copy of the session data.

Before ColdFusion 8, components could not be serialized, so they would not be passed between machines as part of the session. In ColdFusion 8, however, components can be serialized and distributed.

In addition, you can directly call the Java's API to write objects to a file.

Listing 27.8 shows some sample code that checks to see whether a CFC exists in the session scope. If the CFC isn't found in the session, the code checks to see if a serialized copy of the component exists in a file and loads that. Finally, if the CFC is neither in the session nor on the disk, the CFC is instantiated and written out as a new serialized copy.

Listing 27.8. serialize.cfm—Serializing a CFC

  Demonstrate CFC serialization
  Created by Ken Fricklas (
  Modified: 9/10/2007
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Serialize CFC</title>

<cfapplication sessionmanagement="yes" name="serialdemo">
<cfif not isdefined("session.cfcDirector")>
   <!--- check to see if we've got a copy --->
       <cfset fileIn = CreateObject("java", "")>
         <cfset fileIn.init(expandpath("./serialized_director.txt"))>
         <cfset objIn = CreateObject("java", "")>
         <cfset objIn.init(fileIn)>
         <cfset session.cfcDirector = objIn.readObject()>
            <!--- no copy to load, create it --->
            <cfset session.cfcDirector = createObject("component", "director")>
            <!--- save it --->
            <cfset fileOut = CreateObject("java", "")>
            <cfset fileOut.init(expandpath("./serialized_director.txt"))>
            <cfset objOut = CreateObject("java", "")>
            <cfset objOut.init(fileOut)>
            <cfset objOut.writeObject(session.cfcDirector)>
  • + Share This
  • 🔖 Save To Your Account