Understanding the difference between non-blocking web service calls and non-blocking JDBC

I'm trying to understand conceptually why the Play Framework 2.0 thinks it is recommended to use WS.url().get()

for web service calls, but if you complete any other blocking call like a JDBC call in a promise, you are encouraged to do so in an execution context other than the execution context by default?

I understand that by default the Play Framework thread pools are configured so that you have one thread per core and each controller expects to run completely non-blocking code. For this reason, if you are making a blocking call to a controller, say a web service, then you want to make sure that the call does not support the threads available to your controllers.

Otherwise, there will be no thread left for the controllers to execute, because they are all waiting in a blocked state.

But I am confused by the following set of points:

  • First, what thread pool does the controller code itself execute? Is this the default execution context?
  • Secondly, on the one hand, I understand that it is WS.url().get()

    also executed in the default execution context, but on the other hand, the Play Framework Documentation on Thread Pool Configuration states that "note that you might be tempted ... to wrap your blocking code in Futures. That doesn't make it non-blocking, it just means that the blocking will happen on a different thread. "

Does this mean that WS.url().get()

"only happens in a different thread" in the same default execution context? What is the difference between making a JDBC call in a different execution context?

+3


source to share


1 answer


1) Controller replay functions run on the default replay thread pool as described in the linked docs:

Play Default Thread Pool - This is the default thread pool where all application code in the Play Framework runs. This is the Akka dispatcher and can be customized by configuring Akka described below. By default, it has one thread per processor.

Hence, you want to be very careful about blocking inside controller functions, as this will block the thread in the default thread pool.



2) Replaying web services is a non-blocking API, so it doesn't block it ExecutionContext

. This way you can make multiple WS calls in controller functions without blocking the default thread pool. The main difference between a WS call and a JDBC call is that a WS call will not block the thread while waiting for a response from the remote server, the calls are made asynchronously. A JDBC call (like most java IOs) will block the thread while waiting for a response.

Making a JDBC call in another will ExecutionContext

free up the default ExecutionContext

to do other work, allowing the server to handle more requests. You can let Akka handle the hard work of context switching for you. While JDBC calls still block the thread, they at least do not block threads that process requests.

+5


source







All Articles