Object Pooling of VB Object

对象池,是 COM+ 的一个特性.可以避免复杂对象的每次创建.于是很多用vb写的com,就不加更改的加到了com+ 中.其实vb6 写的com都是apartment-threaded ,根本就不支持对象池.

今天跟同时一起确认了一下这个问题.稍微总结一下.

问题:

vb写的一个com,实现了COM+的IObjectControl 或者 ObjectControl,这个跟.net 的enterprise Service 一样. 以为这样可以实现对象池,跟数据库连接池一样.避免每次都创建对象.所以一般都会实现这几个方法

------------------------------------------------

Private Sub ObjectControl_Activate()

End Sub

Private Function ObjectControl_CanBePooled() As Boolean

ObjectControl_CanBePooled = true

End Function

Private Sub ObjectControl_Deactivate()

End Sub

------------------------------------

然后我就想当然的以为这个跟vc或者.net 的实现方式一样. 假定如下:1.组件放到com+中,一定可以设置pool的大小,包括最小池,最大池. 2.如果我设定一个合适的池, 对象多次创建的情况下,应该有部分对象不需要实例化.

然后测试了一下,发现这两点更本就不对. 最后发现vb根本就不支持object pool. 微软的文档原文:object pooling is not possible for components created by vb6.

接下来的问题就是:

既然vb6 不支持object pooling ,那还为什么要实现objectcontrol 这个接口? 而我们的代码也一般都实现了这个接口.

这个问题的回答,我选择了一个com模板被asp调用的例子,并带有解释.

也就是说 对象每一次创建,都会顺序调用构造函数, Activate, 相反,释放的时候也会调用 Deactivate, terminate.

对于activate , 这调用该方法的时候可以活动对象所处的上下文, deactivate 释放该上下文的引用.如果你的com+组件中根本就不访问objectcontext 的话,则不需要该接口.

一般的template 如下:

Option Explicit

'-----------------------------------------------------------­----------------

-----

' Declarations

'-----------------------------------------------------------­----------------

-----

'* Interfaces

Implements COMSVCSLib.ObjectControl

'* Module level variables

Private m_objContext As COMSVCSLib.ObjectContext

Private m_blnMTS As Boolean

Private Const m_ModuleName As String = "nameOfClasshere" 'Used for

error handling

Private m_strUserName As String

Private m_strPassword As String

'-----------------------------------------------------------­----------------

-----

' Properties

'-----------------------------------------------------------­----------------

-----

'-----------------------------------------------------------­----------------

-----

' Methods

'-----------------------------------------------------------­----------------

-----

'-----------------------------------------------------------­----------------

-----

' MTS/COM+ Methods

'-----------------------------------------------------------­----------------

-----

Private Sub ObjectControl_Activate()

'***********************************************************­*******

'* ObjectControl_Activate

'* ----------------------

'* One of the methods of the MTS/COM+ ObjectControl interface. This

subroutine

'* is used to set the objectContext and the MTS/COM+ Boolean.

'***********************************************************­*******

'* Get the Context from the application

Set m_objContext = GetObjectContext()

'* Boolean for MTS/COM+ or not

m_blnMTS = Not (m_objContext Is Nothing)

End Sub

Private Function ObjectControl_CanBePooled() As Boolean

'***********************************************************­*******

'* ObjectControl_CanBePooled

'* -------------------------

'* One of the methods of the MTS/COM+ ObjectControl interface. This

subroutine

'* is used to specify if this object can be pooled. In Visual Basic 6,

this

'* MUST be set to false.

'***********************************************************­*******

'* Must be set to false in Visual Basic 6

ObjectControl_CanBePooled = False

End Function

Private Sub ObjectControl_Deactivate()

'***********************************************************­*******

'* ObjectControl_Deactivate()

'* --------------------------

'* One of the methods of the MTS/COM+ ObjectControl interface. This

subroutine

'* is used to destroy the object. The ObjectContext is set to nothing

here.

'***********************************************************­*******

Set m_objContext = Nothing

End Sub

--------------------