Object Pooling

The concept of object pooling is simple. If an object is marked as pooled, and if the object is not currently being used by any client, then COM+ can reuse this object when a client requests a new object of the same type. This saves the cost of object creation.

Configuration and Working

A serviced component is marked as pooled using a serviced attribute, ObjectPoolingAttribute. When the COM+ application containing the component is first activated, COM+ creates a bunch of objects (the exact number is specified as the minimum pool size) and holds them in a pool. When a client creates an object of the serviced component, COM+ first checks the pool to see if the object of the specified type is available. If so, COM+ activates the object from the pool instead of creating a new one. If the object is not available, COM+ creates a new one, up to the maximum specified size of the pool. When the object is released, either because the client called Dispose on it or as a result of a JIT deactivate-on-return bit set, the object goes back to the pool for reuse.

If all the objects up to the maximum specified limit are already in use when a new object creation request comes in, COM+ waits for a specified timeout interval (configurable from ObjectPoolingAttribute) to see if any of the used objects become available for reuse. If not, the object creation fails.

Note that earlier in the chapter, we deliberately chose the words object activation and deactivation, as opposed to object creation and destruction. The semantic difference between the two should now become clear. When an object is activated, it either gets created or fetched from a pool of existing objects. When an object is deactivated, it either gets destroyed or placed back in the pool.

The ObjectPooling attribute lets you specify all possible settings for your serviced component's object pool. The following code excerpt, for example, enables object pooling for Employee component with a minimum pool size of 2, a maximum pool size of 5, and a creation timeout of 50 milliseconds:

// Project ObjectPooling/Employee

[ObjectPooling(MinPoolSize=2,MaxPoolSize=5,CreationTimeout=50)]
public class Employee : ServicedComponent {
     ...
}

There are times when a pooled object needs some information from COM+ during runtime. For example, a pooled object may need to know when it is getting deactivated so that it can release its resources. To address this, the base class ServicedComponent provides four virtual methods:

public abstract class ServicedComponent : ...
{
     ...

     protected internal virtual void Activate();
     protected internal virtual void Deactivate();
     protected internal virtual bool CanBePooled();
     protected internal virtual void Construct(String s);
}

It is worth noting the ServicedComponent class already provides a default implementation for all four methods. You can simply override the method that you are interested in.

When a serviced component object is being activated, COM+ calls its Activate method before handing the object over to the client. This gives the object a chance to do any necessary initialization.

When the object is being deactivated, COM+ calls the Deactivate method. This is the object's chance to perform whatever cleanup is necessary, such as releasing any held resources, before it is destroyed or recycled.

Immediately after calling Deactivate, COM+ calls the CanBePooled method on the object to check if the object is willing to be pooled for reuse. If the object returns false, COM+ destroys the object. Otherwise, COM+ places (or may place) the object back into the pool.

Note that returning true from CanBePooled does not guarantee that the object will be recycled; it only gives COM+ the permission to recycle it. Returning false, however, guarantees that the object will be destroyed.

Finally, it is possible to pass any extra information as a string to the object at the time of construction. An example where this is useful is to specify a database connection string for an object that needs to connect to a database. COM+ calls the method Construct just after the constructor is called, passing in the construction string.

You can enable construction string support from the Component Services snap-in. This option can be found on the Activation property page of the component properties dialog box. You can also specify the construction string on the same page. A snapshot of the Activation property page is shown in Figure 10.4.

Figure 10.4. Activation properties configuration.


The construction string support can also be enabled on a serviced component by marking it with an attribute, ConstructionEnabledAttribute. In addition, you can specify a default construction string using this attribute, as illustrated in the following code excerpt:

[ConstructionEnabled(Enabled = true,
     Default = "Hello Old World!")]
public class Employee : ServicedComponent {
     ...
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.52.8