Skip to content

Commit f7887f8

Browse files
DiegoKrupitzaschauder
authored andcommitted
Added reference documentation for Lock on derived queries.
The reference documentation now contains how to use `@Lock` on derived queries and what to expect from it. Original pull request #1166
1 parent e84a34a commit f7887f8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/main/asciidoc/jdbc.adoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,3 +1046,32 @@ class Config {
10461046

10471047
If you expose a bean of type `AuditorAware` to the `ApplicationContext`, the auditing infrastructure automatically picks it up and uses it to determine the current user to be set on domain types.
10481048
If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableJdbcAuditing`.
1049+
1050+
[[jdbc.locking]]
1051+
== JDBC Locking
1052+
1053+
Spring Data JDBC currently supports locking on derived query methods. To enable locking on a given derived query method inside a repository, you just need to add the `@Lock` annotation above it.
1054+
1055+
.Using @Lock on derived query method
1056+
====
1057+
[source,java]
1058+
----
1059+
interface UserRepository extends CrudRepository<User, Long> {
1060+
1061+
@Lock(LockMode.PESSIMISTIC_READ)
1062+
List<User> findByLastname(String lastname);
1063+
}
1064+
----
1065+
====
1066+
1067+
As you can see above, the method `findByLastname(String lastname)` will be executed with a pessimistic read lock. If you are using a databse with the MySQL Dialect this will result for example in the following query:
1068+
1069+
.Resulting Sql query for MySQL dialect
1070+
====
1071+
[source,sql]
1072+
----
1073+
Select * from user u where u.lastname = lastname LOCK IN SHARE MODE
1074+
----
1075+
====
1076+
1077+
Alternative to `LockMode.PESSIMISTIC_READ` you can use `LockMode.PESSIMISTIC_WRITE`.

0 commit comments

Comments
 (0)