Three scopes are available to ATG components: global, session, and request. A global component is instantiated once and is shared by all users. A session component is created once for a given session and is shared by all requests of that session. Request scoped components are freshly instantiated for each request that uses them. Resolving the path to an existing component takes a little time, but instantiating a new object is comparatively more costly. To build a high performance web site one must be conscious of the number of objects that are allocated on each request and the amount of memory each user consumes. Therefore it important to think about the scope of a component when it is defined in the system.

In general it is fairly obvious when a component should be globally scoped versus session or request scoped. If a component can be shared, without synchronization problems, by all users, then it should be globally scoped. It is a somewhat more difficult decision to decide if a component should be session or request scoped. The decision balances on minimizing the number of objects that are allocated on every request and reducing the total amount of memory that is used per session. If a component is going to be used on many requests within a session then in general it should be session scoped. However if an object is only going to be occasionally used within the session then it should be declared request scoped. After the request the Java Virtual Machine is allowed to garbage collect the object, versus holding onto it for the life of the session.

Form handlers are a special case of component; they can be used quite often, but in general are request scoped. As a design pattern, messages relayed back to the user (such as error messages) are made available as properties of the form component. It becomes increasingly difficult to manage this sort of component, with messages, as session scoped, because your application needs to be cognizant of when to clear the messages. As a request scoped component the error messages are automatically “cleared” after the request when the object is garbage collected. Notice that the component /atg/commerce/order/ShoppingCartModifier, which manages the shopping cart order, is by default declared to be a request scoped component. Its performance in request scope is sufficient for most sites, but a site that needs an extra performance boost might benefit by changing that form handler to session scoped.

 
loading table of contents...