I have previously published an article at
codeproject.com that describes the design of a connection pool. The source included with that article is not complete, but it still works! I have now taken the next step and finished the implementatin of the connection pool.
The implemenation is divided into three dynamic libraries:
- database.dll
contains the abstraction layer of the pool - adoadapter.dll
contains a concrete ADO implementation of the pool - settings.dll
contains functionality to store the settings in the registry
Compared to the source included with the article at codeproject, this implementation has been enhanced further i.e. :
- the connection string is now stored encrypted in the registry.
- the abstraction layer has been put into its own dynamic link library. This makes it possible to encapsulate the vendor specific implementation and it becomes easier to change from one database to another.
- the methods aquire() and release() are now made thread-safe.
- the storing logic has been moved into a separate dynamic link library, which makes it possible to store the settings another place if a dynamic link library with the same interface is made by deriving the implementation from the abstract CPoolSettings class.
The snippet below illustrates briefly how the connection pool is used.
-- snip --
#include "database.h"
#include "adoadapter.h"
#import "msado15.dll" rename_namespace("")
rename("EOF", "ADO_EOF") rename("BOF", "ADO_BOF")
CPoolFactory * factory = new CADOPoolFactory;
CPool * pool = factory->getPool();
CResource * res = pool->aquire();
// use the connection, i.e.
// recordset->Open("SELECT STMT",
// res->getConnectionVariant() /* we use it here */,
// adOpenDynamic, adLockOptimistic, adCmdText);
// ...
// recordset->Close()
pool->release(res);
res = NULL;
-- snip --
First time the pool is used it will store the initial size and an encrypted connection string (with default values) in the registry. The connection string that containts the default values are written in plaintext to a file named "pool-regvalues.txt", which easily can be modified and pasted into the registry.
Another (and maybe incomplete) implementation of the connection pool in C# can be found at one of my elderly posts, which can be accessed
here.
Download C++ library(if the link fails by left-clicking on it, then right-click and choose "Save target as..." or "Copy link address")