Non-blocking Queries in Spring Data for MongoDB?
Asked Answered
L

1

6

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. ;)

Lamphere answered 25/1, 2016 at 23:8 Comment(2)
what was your solution in the end ?Warrant
@Warrant I've been using the reactive api to solve my concrete problem, cf. my answer below.Lamphere
L
2

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."

Lamphere answered 24/9, 2019 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.