One way to increase scalability of the server application is to run IO-bound operation (reading files, sockets, web requests, database requests etc) asynchronously. This does not mean run them in the ThreadPool which will just block threads while operation is being executed. The correct way is to use asynchronous API (BeginRead, BeginGetResponse, BeginExecuteReader etc). The problem is well described in CLR vi C# book.
Here is some article about asynchronous queries in Linq to SQL.
Are any ways to execute Nhibernate query asynchonously? What about Linq to NHibernate?
Thank you, Andrey
Note that async database calls do NOT imply better overall scalability by themselves. I recommend reading the article "Should my database calls be Asynchronous?" for an in-depth analysis. Here's a quote from that article:
Although there is still no support for async queries in NH, you can still partially overcome some of the undesired effects of running (long-running) db calls from request thread.
What you want is to split Threadpool between short-running and long-running operations. Of course this is not possible with actual implementation of Threadpool and TPL but you can help yourself quite eassilly by writing your own Producer/Consumer queue with awaitable items and customized concurency.
Please have a look at example i have put together : https://gist.github.com/3746240
Code is copy/pasted from great book "C# 5.0 in a Nutshell: The Definitive Reference" by Joseph Albahari and Ben Albahari with modification done by me causing the scheduler to create dedicated worker threads for items proccesing.
As of NHibernate v5, async is now fully supported!
Here are some nifty examples:
Updating an entity
Source article
Unfortunately, no. NHibernate does not expose the internal implementation of the command execution in the way L2S does.
You'll have to use the threadpool OR create a patch for NH to add asynchronous query support. That would be very welcome by the community and would make for a nice exercise (but it's not trivial at all)
multiple async calls can be rewritten with Futures
can be replaced with
this even has the benefit over async code that the roundtrip time is only paid once instead of 3 times.