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 java.io.ObjectOutputStream 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

<!---
  serialize.cfm
  Demonstrate CFC serialization
  Created by Ken Fricklas (kenf@fricklas.com)
  Modified: 9/10/2007
--->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Serialize CFC</title>
</head>

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

Related Resources

There are currently no related titles. Please check back later.