Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Intermediate
Dec 10, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Take advantage of COM+ object pooling

VB6 objects can't be pooled under COM+, because they are apartment threaded. This restriction is void with VB.NET objects (and all .NET objects in general), because they are free-threaded. To make an object poolable you just need to decorate the class with the ObjectPooling attribute:

Imports System.EnterpriseServices

<ObjectPooling()> Public Class BankTransfer 
    Inherits ServicedComponents
    ' ...
End Class
The attribute's constructor is overloaded to let you initialize the Enabled, MaxPoolSize, and MinPoolSize properties:

' enable object pooling, with MaxPoolSize set to 100, and MinPoolSize set to 10
<ObjectPooling(True, 100, 10)> 
The default value for MinPoolSize is 0, the default value for MaxPoolSize is 1,048,576. You can also set the CreationTimeout property, whose default value is 60 milliseconds, which indicates the time to wait for an object to become available in the pool before throwing an exception:

' as in previous example, but set a timeout of 10 milliseconds
<ObjectPooling(True, 100, 10, CreationTimeout:=10)> 
Notice that all serviced components implement the IDisposable interface. A client should call Dispose as soon as the object isn't used any longer, because the Dispose method 's implementation of a serviced component maps to the ServicedComponent.DisposeObject shared method, which puts the disposed object back in the pool.

Another technique for automatically put the object back to the pool when clients don't need it any longer, and that works without having the client explicitly call the Dispose method, is applying the JustInTimeActivation attribute to make the component support JIT activation. In this case COM+ puts the object back to the pool at the end of any method call that sets the "done" bit. This can be done very easily if you mark a method with the AutoComplete attribute:

<JustInTimeActivation(True), ObjectPooling(True)> Public Class BankTransfer 
    Inherits ServicedComponents
    <AutoComplete()> Sub Transfer(fromAccount As String, toAccount As String, _
        Money As Decimal)
        ' ...
        ' this object is put back in the pool when this method completes
    End Sub    

End Class

Francesco Balena
Thanks for your registration, follow us on our social networks to keep up-to-date