Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking Queries in Spring Data for MongoDB?

I want to execute non-blocking database queries through Spring Data accessing a MongoDB using MongoDB's Async Client API.

So far I've only seen the possibility to return a

  • java.util.concurrent.Future
  • java.util.concurrent.CompletableFuture
  • org.springframework.util.concurrent.ListenableFuture

and annotate the query method with @Async, e.g.

public interface UserRepo extends Repository<User, Long> {

  @Async
  ListenableFuture<User> findByName(String name);

}

but the documentation clearly states that the actual [...] query execution will occur in a task that has been submitted to a Spring TaskExecutor. So it's not really non-blocking but just decoupling my thread using a thread pool which doesn't scale very well.

Therefore my question:

How to execute the queries in non-blocking mode using the MongoDB Async Driver's NIO Features?

The only workaround I see so far is to get rid of Spring Data and implement the database queries by my own using the Mongo Async Driver API. But hopefully I'm just missing something and there is a straigth-forward answer out there. ;)

like image 981
Marc-Christian Schulze Avatar asked Jan 25 '16 23:01

Marc-Christian Schulze


1 Answers

Starting with Spring Data Kay M1 finally a reactive API has been introduced that allows non-blocking data access using the ReactiveCrudRepository interface.
There's a good blog post of the Spring team about it.
It's nowadays also mentioned in the Spring Data documentation but keep in mind if you already started your project by using the @Async feature to not mix it with the new reactive api as the docs clearly states: "Asynchronous query execution is different from reactive query execution and should not be mixed."

like image 80
Marc-Christian Schulze Avatar answered Oct 12 '22 11:10

Marc-Christian Schulze