My headache is this – my server application exceeds the maximum open database connections while being under load. So, I figure I need a task queue (aka service bus) for write database access. A queue, that db write requests can be posted to it and the dedicate threads will read it and execute.
I was wondering if there are ready components out there that do just that. My requirements are:
- Multiple threads can write to the queue with minimum blocking.
- More than one thread can read from the queue to execute the posted write requests. In the most constrained case this number is 1, but it may be more, for instance 10% of the open db connection limit.
Any ideas?
Thanks.
P.S.
I have noticed this, and this, but neither seem relevant to me.
Using a
BlockingCollection<T>, which implements a producer/consumer pattern. It’s thread safe, so you can access it from multiple threads simultaneously. Here’s an example of how that would be used:Note that the above implementation could also be performed in reverse, with a background
Taskconsuming from the collection and the main worker thread filling it.Joseph Albahari has an excellent writeup on Tasks and parallelism. You can read all about it, and specifically about BlockingCollection, here.