Skip to content

Commit 43ff050

Browse files
vmikhailenkogithub-actions[bot]agoerlerrenejeglinsky
authoredJan 22, 2024
Document limitations for Select ... lock() (#630)
* Document limitations for Select ... lock() * Update java/query-api.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * improve wording about calculated elements * improve wording about localized entities * improve wording about calculated elements * Adopt feedback * Mention subqueries * editing --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Adrian Görler <adrian.goerler@sap.com> Co-authored-by: Rene Jeglinsky <rene.jeglinsky@sap.com>
1 parent bab4382 commit 43ff050

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
 

‎java/query-api.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -750,14 +750,29 @@ Select.from("bookshop.Books").byId(1).lock(5);
750750
Update.entity("bookshop.Books").data("price", 18).byId(1);
751751
```
752752
753-
To set a _shared_ (read) lock specify the lock mode `SHARED` in the lock method:
753+
To set a _shared_ (read) lock, specify the lock mode `SHARED` in the lock method:
754754
755755
```java
756756
import static com.sap.cds.ql.cqn.CqnLock.Mode.SHARED;
757757
758758
Select.from("bookshop.Books").byId(1).lock(SHARED);
759759
```
760760
761+
Not every entity exposed via a CDS entity can be locked with the `lock()` clause. To use the `lock()` clause, databases require that the target of such statements is represented by one of the following:
762+
- a single table
763+
- a simple view, so that the database can unambiguously identify which rows to lock
764+
765+
Views that use joins, aggregate data, include calculated or coalesced fields cannot be locked. Some databases might have additional restrictions or limitations specific to them.
766+
767+
There are few notable examples of such restrictions:
768+
769+
* You cannot use the `lock()` together with a `distinct()` or a `groupBy()`.
770+
* You cannot use the `lock()` in a statement with the subquery as a source.
771+
* Localized entities can be locked only if your query is executed without a locale, as described in the chapter [Modifying Request Context](./request-contexts#modifying-requestcontext).
772+
* Entities that contain "on-read" calculated elements can't be locked when the statement references them in the select list or a filter.
773+
774+
As a general rule, prefer the statements that select primary keys with a simple condition, such as `byId` or `matching`, to select the target entity set that is locked.
775+
761776
## Insert
762777

763778
The [Insert](../cds/cqn#insert) statement inserts new data into a target entity set.

0 commit comments

Comments
 (0)
Please sign in to comment.