.NET Framework 开发员指南 对象池

COM+ 对象池服务可以减少从头创建每个对象的系统开销。在激活对象时,它从池中提取。在停用对象时,它放回池中,等待下一个请求。

可以这样配置对象池:将 ObjectPoolingAttribute 属性应用于从 System.EnterpriseServices.ServicedComponent 类派生的类。

对象池使您能够控制所使用的连接数量,与连接池相反,连接池用来控制达到的最大数量。下面是对象池和连接池之间的重要区别:

  • 创建。使用连接池时,创建在同一线程上进行,因此如果池中没有连接,则代表您创建连接。采用对象池时,池可以决定创建新对象。但是,如果已经达到最大数量,它会给您下一个可用的对象。当需要花费较长时间来创建对象时,这的确是一个重要的行为。但不要长期使用这种方法来创建对象。
  • 最小值和最大值的实施。不在连接池中实施。对象池的最大值在尝试缩放应用程序时很重要。可能需要仅为几个对象而复用成千上万个请求。(TPC/C 基准依赖这一功能。)

COM+ 对象池与 .NET Framework 托管 SQL 客户端连接池几乎完全相同。例如,创建在不同的线程上进行,并强制实施最小值和最大值。

注意   应用程序域会影响对象池的行为。在 Microsoft Windows 2000 中,当应用程序激活设置为 Library 并且您有多个应用程序域时,将在默认应用程序域中创建所有缓冲池对象,并在多个客户端之间共享。在同样的情况下,当使用 Microsoft Windows XP 和 Windows Server 2003 时,每个应用程序域都有一个对象池。无论使用哪一种操作系统,当有多个应用程序域,且应用程序激活设置为服务器时,进程外客户端将使用默认应用程序域中的对象池。

下面的示例针对 TestObjectPooling 类设置大小和超时限制:

[Visual Basic]
<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;
      }
}

客户端

[Visual Basic]
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+ 对象池服务时没有启用实时 (JIT) 激活,则必须从客户端调用 DisposeObject。这种情况下,COM+ 需要在您使用完对象时收到通知,以验证将对象返回到池中是否安全。通常,如果打算一次只调用一个缓冲池对象,则最好对对象池启用 JIT 激活。如果打算获取引用并多次调用它,使用没有启用 JIT 激活的对象池可能会提高性能。
 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值