简体   繁体   中英

Using async-await for database queries

I'm currently working on an ASP NET web api project, which has all its calls to the database in an asynchronous way.

We are using ServiceStack.OrmLite , which provides us with an asynchronous and synchronous API to make the database calls.

Example we are doing this:

Option 1

var activity = await context.SingleByIdAsync<Activity>(x => x.Id == myId);
var place = await context.SingleByIdAsync<Place>(x => x.Id == myId);

But we could also implement it this way.

Option 2

var activity = context.SingleById<Activity>(x => x.Id == myId);
var place = context.SingleById<Place>(x => x.Id == myId);

From what little I understand of asynchronous programming, with option 1 we are blocking the thread and releasing it when the operation is completed.

I think that option 1 is more expensive the way we are implementing it than option 2.

Go into a blocking wait - Thread.Sleep, Task.Wait, etc. This means that you fire your database request on thread A and then enter a wait on that thread: the thread is allocated but is blocked and not usable for anything else. When your data is ready, you somehow learn of that and exit the wait and your code continues on thread A.

  1. What is the best approach?

  2. What is the benefit of using asynchronous operations for database calls?

  3. Are we implementing asynchronous calls properly?

  4. Will I have a problem if I switch all calls to the database to synchronous?

  1. What is the best approach?

    This is hard to tell by so little code, but in general if you are willing to change all the methods in the call-stack and there is a proper async methods available use it.

    What I mean by change all the methods in the call-stack is, if you change method TA() to be async (ie async Task<T> AAsync() then you will have to also change any method calling A to be also async.

    What I mean by proper async method : In the case of ASP.Net there is no UI-Thread so it would be unimportant which thread would be blocked by a synchronous method. So in this context a proper async method would be one that does some async IO and does not block any thread (not even some thread-pool-thread).

  2. What is the benefit of using asynchronous operations for database calls?

    In generell the benefit of using async in ASP.Net is that while a request is asynchronously waiting for some (proper) async operation to finish, it does not block any thread from processing another request.

    So as long the packaged you use, implements the database calls as proper async IO you will have the benefit of better scalability of your server (ie it can process more request in parallel).

  3. Are we implementing asynchronous calls properly?

    It is hard to tell from so little code, but as long you don't use Task.Wait() in the call stack you are probably using to correct.

  4. Will I have a problem if I switch all calls to the database to synchronous?

    As said in 2. you could get problems when two many request are done at the same time.

You said "From what little I understand of asynchronous programming, with option 1 we are blocking the thread and releasing it when the operation is completed." , it is the opposite. When a thread comes to an await it will asynchronously wait for the awaited tasks to be finished. That means that the thread will be free to do other things. A task that finishes or fails (with an exception) will be scheduled to be continued. Where this is depends on the captured synchronization context .

You also said "I think that option 1 is more expensive the way we are implementing it than option 2." , that is not enterily wrong since async methods are build into a state machine and will have a little bit of overhead. But since you are in an ASP.Net context you will greatly benefit of using the async operations to free thread to work on other requests.

Here is an article on using async on ASP.Net.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM