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 4

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

Real-world Example
So now you might be asking, When would I want to actually use the SPM? Good question. There are a couple different scenarios. You could use the SPM to store database connection strings. Connection strings typically don’t change between releases of an application so this is a valid candidate for using the SPM. Or let’s say you have several methods of a component that need to read configuration-type data from XML at run-time. For this, you could load the XML string into the SPM for quick access during the method calls. These aren’t the only uses, but this article should give you the knowledge of what to store in the SPM and when to put it there.

Back to storing database connection strings in the SPM. I have a DLL named SPMVB6.dll that contains one class module—MyDatabase.cls. This component is used to connect to a SQL Server database; its connection string is stored as a global private constant. The connection string could have easily been read at object construction or stored in a .udl or XML file, etc., but it’s not really important where the connection string starts out at—only where it ends up. Also included in the component are a couple methods used to manage data the SPM.

The design of the MyDatabase component is as such: the MyDatabase.cls module uses Class_Initialize to put the connection string in the SPM. The OpenDBConnection method then retrieves the connection string from the SPM to establish database connectivity. Because this article focuses on the SPM and not database connections, I’ve not provided a method to close the database connection or anything else related to the database connection. The OpenDBConnection method makes use of the SPM methods, which are what you really want to see. There are numerous ways to manage database connections, but that is not the topic of this article.

Note about the following code: no error handling routines have been added. The code is kept as thin as possible for the purpose of this article. Most, if not all of the code in this article, can be used out of the box, but be sure to add in your own error handling.

That being said, here’s all the code for the MyDatabase.cls module:

Option Explicit Private Const CONN_STR As String = "Provider=SQLOLEDB.1;" & _

Dave Donaldson is a Microsoft technology writer.
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