Habarisoft introduces dynamic connection pooling with the next release of its message broker client libraries for Delphi and Free Pascal. Connection pools save resources in multi-threaded applications, as connections are initialized only once and then shared between threads. They can speed up the program execution because the pool returns and stored them in started state.
The key parts of thread-safe connection pooling with Habari libraries are the classes TBTConnectionPool and TPoolableConnectionFactory. The connection pool class provides two methods:
- BorrowConnection – this method returns a started IConnection instance
- ReturnConnection – this method returns the instance back to the pool
These two methods are best used with a try … finally block to ensure that the connection will be returned to the pool:
try Conn := Pool.BorrowConnection; ... use the connection finally Pool.ReturnConnection(Conn); end;
Creating a connection pool
Creating the pool is easy and requires only one line of code. After instantiating the poolable connection factory
Factory := TBTPoolableConnectionFactory.Create(...);
the pool can be created with this factory, an initial pool size, and dynamic sizing with this line of code:
Pool := TBTConnectionPool.Create(Factory, 2, eaGrow);
This early implementation has some limitations, but all registered users of Habari Client libraries can receive the source code to try it and to give feedback.