物件共用
COM+ 物件共用服務,可讓您減少從頭建立每一個物件的多餘負擔。當物件啟動時,會將它從集區中提取出來。當物件停用時,會將它放回集區中等待下一次要求。
您可以將 ObjectPoolingAttribute 屬性套用於從 System.EnterpriseServices.ServicedComponent 類別衍生而來的類別,即可設定物件共用。
物件共用可讓您控制使用的連接數目,這和連接共用 (Connection Pooling) 不同,後者是讓您控制連線的最大數目。下列是物件共用和連接共用之間的重要差異:
- **建立。**使用連接共用時,建立是在相同的執行緒上進行,因此,如果集區是空的,就會為您建立連接。而使用物件共用時,集區可能會決定建立新的物件。但是,如果您已經達到最大的連接數目,它就會為您提供下一個可用的物件。如果需要花很長的時間才能建立物件,但是使用的時間並不長,這便是一個很重要的方式。
- **強制最小值和最大值。**這不會在連接共用中進行。當您嘗試縮放應用程式時,物件共用中的最大值非常重要。您可能會需要多工處理對少數物件的數千個要求。(TPC/C 基準即依賴這點)
COM+ 物件共用實際上和 .NET Framework Managed SQL 用戶端連接共用是相同的。例如,建立是在不同的執行緒上進行,並且強制使用最小值和最大值。
注意 應用程式定義域會影響物件共用的行為。當 Microsoft Windows 2000 中的應用程式啟動設定為 Library,而且您有多個應用程式定義域時,共用物件全部都會在預設的應用程式定義域中建立,並且由多個用戶端共用。同樣的,使用 Microsoft Windows XP 和 Windows Server 2003 時,每個應用程式定義域都有一個物件集區 (Object Pool)。如果您使用的任一作業系統有多個應用程式定義域而且應用程式啟動設定為伺服器,跨處理序 (Out-Of-Process) 用戶端會使用預設應用程式定義域中的物件集區。
下列範例會設定 TestObjectPooling
類別的大小和逾時限制:
<ObjectPooling(MinPoolSize := 2, MaxPoolSize := 5, _
CreationTimeout := 20000)> _
Public Class TestObjectPooling
Inherits ServicedComponent
Public Sub Perform ()
' Method contents go here.
End Sub
Public Overrides Sub Activate()
' Called when removed from the pool.
End Sub
Public Overrides Sub Deactivate()
' Called before deactivating or placing back in pool.
End Sub
Public Overrides Function CanBePooled() As Boolean
' Called after Deactivate. Indicate your vote here.
Return True
End Function
End Class
[C#]
[ObjectPooling(Enabled=true, MinPoolSize=2, MaxPoolSize=5, CreationTimeOut=20000)]
public class TestObjectPooling : ServicedComponent
{
public void Perform ()
{
// Method contents go here.
}
public override void Activate()
{
// Called when removed from the pool.
}
public override void Deactivate()
{
// Called before deactivating or placing back in pool.
}
public override bool CanBePooled()
{
// Called after Deactivate. Indicate your vote here.
return true;
}
}
用戶端
Public Class App
Overloads Public Shared Sub Main(args() As String)
Dim order As New TestObjectPooling()
order.Perform()
' To return the object to the object pool, use DisposeObject.
' This returns the object to the pool and allows it to be reused.
' If this call is not made, the garbage collector returns it to the pool
' in a non-deterministic fashion, which hinders performance
' of an application that depends on object pooling to conserve
' expensive resources.
ServicedComponent.DisposeObject (order)
End Sub
End Class
[C#]
public class App
{
public static int Main(string[] args)
{
TestObjectPooling order = new TestObjectPooling();
order.Perform();
/* To return the object to the object pool, use DisposeObject.
This returns the object to the pool and allows it to be reused.
If this call is not made, the garbage collector returns it to the pool
in a non-deterministic fashion, which hinders performance
of an application that depends on object pooling to conserve
expensive resources. */
ServicedComponent.DisposeObject (order);
}
}
注意 一般而言,使用服務元件時,通常不必從用戶端呼叫 DisposeObject。但使用 COM+ 物件共用服務時,如果沒有啟用 Just-in-Time (JIT) 啟動服務,就必須呼叫。此時,為驗證能夠安全的將物件傳回集區,您必須在完成物件時通知 COM+。一般而言,如果您一次只呼叫一個共用物件,通常最好讓物件共用啟用 JIT 啟動。但如果您不但要參考而且還要建立多重呼叫,則使用不執行 JIT 啟動的物件共用,效能較佳。
請參閱
可用的 COM+ 服務摘要 | ObjectPoolingAttribute | System.EnterpriseServices 命名空間