The COM+ Object Pooling service enables you to reduce the overhead of creating each object from scratch. When an object is activated, it is pulled from the pool. When the object is deactivated, it is placed back into the pool to await the next request.
You can configure object pooling by applying the ObjectPoolingAttribute attribute to a class that derives from the System.EnterpriseServices.ServicedComponent class.
Object pooling lets you control the number of connections you use, as opposed to connection pooling, where you control the maximum number reached. Following are important differences between object pooling and connection pooling:
- Creation. When using connection pooling, creation is on the same thread, so if there is nothing in the pool, a connection is created on your behalf. With object pooling, the pool might decide to create a new object. However, if you have already reached your maximum, it instead gives you the next available object. This is crucial behavior when it takes a long time to create an object, but you do not use it for very long.
- Enforcement of minimums and maximums. This is not done in connection pooling. The maximum value in object pooling is very important when trying to scale your application. You might need to multiplex thousands of requests to just a few objects. (TPC/C benchmarks rely on this.)
Note Application domains affect the behavior of object pooling. In Microsoft Windows 2000, when application activation is set to Library and you have multiple application domains, pooled objects are all created in the default application domain and shared between multiple clients. In the same situation, using Microsoft Windows XP and Windows Server 2003, there is one object pool per application domain. Using either operating system with multiple application domains and your application activation set to server, out-of-process clients use the object pool in the default application domain. By.http://msdn.microsoft.com
Code 1: Object Pool and Employee class.
using System;
using System.Collections;
namespace ObjectPooling
{
class Factory
{
// Maximum objects allowed!
private static int _PoolMaxSize = 2;
// My Collection Pool
private static readonly Queue objPool = new Queue(_PoolMaxSize);
public Employee GetEmployee()
{
Employee oEmployee;
// check from the collection pool. If exists return object else create new
if (Employee.Counter >= _PoolMaxSize && objPool.Count>0)
{
// Retrieve from pool
oEmployee = RetrieveFromPool();
}
else
{
oEmployee = GetNewEmployee();
}
return oEmployee;
}
private Employee GetNewEmployee()
{
// Creates a new employee
Employee oEmp = new Employee();
objPool.Enqueue(oEmp);
return oEmp;
}
protected Employee RetrieveFromPool()
{
Employee oEmp;
// if there is any objects in my collection
if (objPool.Count>0)
{
oEmp = (Employee)objPool.Dequeue();
Employee.Counter--;
}
else
{
// return a new object
oEmp = new Employee();
}
return oEmp;
}
}
class Employee
{
public static int Counter = 0;
public Employee()
{
++Counter;
}
private string _Firstname;
public string Firstname
{
get
{
return _Firstname;
}
set
{
_Firstname = value;
}
}
}
}
Code 2: How to use it?
private void button1_Click(object sender, EventArgs e)
{
Factory fa = new Factory();
Employee myEmp = fa.GetEmployee();
Console.WriteLine("First object");
Employee myEmp1 = fa.GetEmployee();
Console.WriteLine("Second object")
}
No comments:
Post a Comment