Browse DevX
Sign up for e-mail newsletters from DevX


Using the COM+ Shared Property Manager with Visual Basic 6: Caching Data in the Middle-Tier : Page 2

This article describes one of the most underused features of COM+—the Shared Property Manager (SPM). The SPM can store frequently accessed data in the middle-tier, thus creating a caching mechanism that can be used by any component running in a COM+ application. By the end of this article, youll know how and when to use the SPM, as well as knowing its pros and cons.




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

Creating SPM Groups
Before you can store data in the SPM, you must create an SPM group to hold your data. This is done as such:

Const GRP_NAME As String = "MyGroup" Dim blnExists As Boolean Dim spmMgr As COMSVCSLib.SharedPropertyGroupManager Dim spmGrp As COMSVCSLib.SharedPropertyGroup Set spmMgr = New COMSVCSLib.SharedPropertyGroupManager ' create the SPM group Set spmGrp = spmMgr.CreatePropertyGroup(GRP_NAME, LockSetGet, _ Process, blnExists)

The function definition for CreatePropertyGroup looks like this:

CreatePropertyGroup(NameOfGroup, IsolationMode, ReleaseMode, ExistsFlag)

The CreatePropertyGroup method requires some interesting parameters. NameOfGroup is self-explanatory, but the other three need some clarification. IsolationMode determines the locking mechanism used by the SPM group. The ReleaseMode parameter determines how long data in the SPM group remains in memory. ExistsFlag is an output value that tells the caller if the group already existed in memory. The IsolationMode and ReleaseMode parameters require further explanation, which is done later in this article.

At this point, it’s interesting to note that calls to the SPM are slightly different than most. To create a group or property in the SPM, you use a corresponding "create" method. To check if an SPM group or property already exists, you use the same methods. This explains the purpose of the ExistsFlag parameter. In the above example, a call to CreatePropertyGroup returns blnExists. If this value is False, the group was created in the SPM. A value of True means the group already exists in memory; therefore, it will not be created again. The reasoning behind this is simple: if a group or property is constantly being created in the SPM, the whole purpose of caching is defeated.

Now that you’ve got your group, let’s put some data in it.

Creating SPM Properties
Data values that are put into SPM groups are called properties. The code below demonstrates how to create a property in a given SPM group:

Const GRP_NAME As String = "MyGroup" Const PRP_NAME As String = "MyProperty" Dim spmMgr As COMSVCSLib.SharedPropertyGroupManager Dim spmGrp As COMSVCSLib.SharedPropertyGroup Dim spmPrp As COMSVCSLib.SharedProperty Dim blnGrpExists As Boolean Dim blnPrpExists As Boolean Set spmMgr = New COMSVCSLib.SharedPropertyGroupManager ' create the SPM group; remember that the group might already exist Set spmGrp = spmMgr.CreatePropertyGroup(GRP_NAME, _ LockSetGet, Process, blnGrpExists) ' create the SPM property; the property might also already exist Set spmPrp = spmGrp.CreateProperty(PRP_NAME, blnPrpExists)

The function definition for CreateProperty looks like this:

CreateProperty(NameOfProperty, ExistsFlag)

The NameOfProperty parameter is easy, and ExistsFlag is used in the same fashion as mentioned above.

So, you’ve got a group that now contains a property, but the property doesn’t have a value. Let’s take a look at how to set and get property values.

Getting and Setting Property Values
Fortunately, getting and setting SPM property values is very simple. Once you have a group and property created, the code below can be used to get and set property values (assuming the variables have already been declared):

' get property value vntValue = spmPrp.Value ' set property value spmPrp.Value = vntValue

It’s important to note that all values assigned to SPM properties are of type Variant. This allows you to store simple data values in the SPM, including arrays. To assign an array to a property, you could use code similar to this:

Dim arrData(0 To 3) As String arrData(0) = "A" arrData(1) = "B" arrData(2) = "C" arrData(3) = "D" spmPrp.Value = arrData

You must resist the temptation to put Visual Basic object references in the SPM. That can lead to dangerous and unexpected results because Visual Basic objects use the STA threading model; therefore, caching those references could greatly reduce concurrency. The flip-side of that is storing MTA objects references (C++ components) in the SPM because concurrency does not become an issue. As an example, storing a reference to the Microsoft FreeThreadedDOMDocument in the SPM would be safe because it is an MTA object. For more information, please read Ted Pattison’s book on programming COM+ with Visual Basic (mentioned again at the end of this article).


Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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