Add ReadPreference annotation.

The annotation enables a declarative style of defining the ReadPreference to use for individual or all repository queries.

Closes: #2971
Original Pull Request: #4503
This commit is contained in:
Jorge Rodríguez Martín
2023-09-13 18:30:46 +02:00
committed by Christoph Strobl
parent 0474f65044
commit 16b97a26cc
11 changed files with 418 additions and 1 deletions

View File

@@ -824,3 +824,33 @@ interface GameRepository extends Repository<Game, String> {
<2> Instead of `@Query(collation=...)`.
<3> Favors `@Collation` over meta usage.
====
== Read Preferences
The `@ReadPreference` annotation allows you to configure MongoDB's ReadPreferences
.Example of read preferences
====
[source,java]
----
@ReadPreference(
value = "primaryPreferred",
tags = {@ReadPreferenceTag(name = "local", value = "east"), @ReadPreferenceTag(name = "pre", value = "west")},
maxStalenessSeconds = 150
) <1>
public interface PersonRepository extends CrudRepository<Person, String> {
@ReadPreference(value = "secondaryPreferred") <2>
List<Person> findWithReadPreferenceAnnotationByLastname(String lastname);
@Query(readPreference = "nearest") <3>
List<Person> findWithReadPreferenceAtTagByFirstname(String firstname);
List<Person> findWithReadPreferenceAtTagByFirstname(String firstname); <4>
----
<1> Configure read preference for all repository operations that do not have a query-level definition. Therefore, in this case the read preference mode will be `primaryPreferred`
<2> Use the read preference mode defined in annotation `ReadPreference`, in this case secondaryPreferred
<3> The `@Query` annotation defines the `read preference mode` alias which is equivalent to adding the `@ReadPreference` annotation.
<4> This query will use the read preference mode defined in the repository.
====