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:
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:
var collection = new BlockingCollection<MyClass>();
// start a new background task to fill the queue
Task.Factory.StartNew(() => {
while(thereIsStillStuffToStoreInTheDatabase)
{
var item = GetNextItem();
collection.Add(item);
}
collection.CompleteAdding();
});
// GetConsumingEnumerable() blocks while empty until the producer
// has more items or it signals that adding is complete
foreach(var item in collection.GetConsumingEnumerable())
{
// store item in database
}
Note that the above implementation could also be performed in reverse, with a background Task consuming 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With