Resource Dispensers

There is a popular misconception that resource managers and resource dispensers are pretty much the same sort of thing, the only difference being that resource dispensers only deal with non-durable (i.e. memory-bound) data. However, if you have ever gone through the process of building your own resource manager and dispenser (as we all will, by the time we get to the end of Chapter 7), you will realize that they are different beasts altogether.

Here is the difference between the two:

The two often work hand in hand. Indeed, because of the way MTS is constructed, it’s only resource dispensers that actually get access to the transaction context when the transaction is generated automatically by MTS. We’ll leave the technicalities of this until Chapter 7, but for now, let’s take another look at that ADO connection instantiation. Remember that ADO sits on top of ODBC? Well, it so happens that, within MTS, connections to ODBC are controlled by the ODBC Driver Manager, which is an MTS Resource Dispenser. So, when we instantiate our ADO connection object, we are actually getting hold of an ODBC connection resource. If we are operating within a transaction, the ODBC resource dispenser can get hold of the transaction context, and hence participate in the transaction commit process.

What else is the ODBC resource dispenser doing for us here? Let’s say that our component is actually being used over and over again, very quickly. Do we really want the hassle of setting up the connection to SQL Server every time? No, what we’d really like would be to hang on to the connection, reset it and use it for the next invocation. This is what the ODBC resource dispenser does. Whenever a component drops a connection to a SQL Server database, the resource dispenser resets it, and sets a timer running. If the timer expires, it does indeed drop the connection to the database; however, if another component gets in before the timer expires, it simply picks up the existing connection.

This re-use of resources is a bit like the system on the Japanese underground where umbrellas are left around for commuters to pick up if they have forgotten theirs at home. It saves the hassle of buying another one, and – provided that the borrowed umbrellas are returned to the pool after use — it works admirably. The Dutch tried a similar system with white bicycles in Amsterdam in the sixties. However, most of the bicycles eventually ended up in the bottom of the canals — a sad comment, perhaps, on the ultimate comparative success of hippy idealism and Confucianism.

What else can resource dispensers be used for? If we want to share state between two instances of the same object for some reason, we can use a resource dispenser to control access to that shared area. The resource dispenser that handles this is called the Shared Property Manager, better known as SPM (pronounced “Spam”).

© 1998 by Wrox Press. All rights reserved.