I am creating a simple Indy webserver using TIdHTTPServer. On almost all request the server needs to communicates with a database (via TAdoConnection). Seeing that database connections are somewhat expensive in terms of resources, I would like to create a pooling mechanism to reuse connections; rather than making the connection on each request.
I have searched unsuccessfully for examples. This Link on Embarcadero Discussion Forums suggests sub-classing descendents of TIdSchedulerOfThreadPool and TIdThreadWithTask. But I still cannot get it all together.
Will I need to override the TIdSchedulerOfThreadPool.NewThread method and have it return my sub-classed TIdThreadWithTask object which in turn would have it’s own TAdoConnection object?
Does anyone have an example? Should I not even worry about this and just open the database connection on each request?
Why don’t you manage the pool yourself?
You have a list of connections. It starts empty. Every time a request comes, you look for available connections (connections which are active but not being used). If none are found, you create one and put it in the list, as unavailable. Once the request ends, you set the connection to available.
Been there, done that, do not regret doing it at all! A few points of concern: