Skip to content

Commit 340e8c6

Browse files
committed
Clarify transactional defaults and recommendations.
Closes #1000
1 parent 95b2db5 commit 340e8c6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/asciidoc/jdbc.adoc

+6-2
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ Thus, if you want to inspect what SQL statements are run, activate logging for S
920920
[[jdbc.transactions]]
921921
== Transactionality
922922

923-
CRUD methods on repository instances are transactional by default.
923+
The methods of `CrudRepository` instances are transactional by default.
924924
For reading operations, the transaction configuration `readOnly` flag is set to `true`.
925925
All others are configured with a plain `@Transactional` annotation so that default transaction configuration applies.
926926
For details, see the Javadoc of link:{javadoc-base}org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.html[`SimpleJdbcRepository`].
@@ -1008,7 +1008,11 @@ Typically, you want the `readOnly` flag to be set to true, because most of the q
10081008
In contrast to that, `deleteInactiveUsers()` uses the `@Modifying` annotation and overrides the transaction configuration.
10091009
Thus, the method is with the `readOnly` flag set to `false`.
10101010

1011-
NOTE: It is definitely reasonable to use transactions for read-only queries, and we can mark them as such by setting the `readOnly` flag.
1011+
NOTE: It is highly recommended to make query methods transactional. These methods might execute more then one query in order to populate an entity.
1012+
Without a common transaction Spring Data JDBC executes the queries in different connections.
1013+
This may put excessive strain on the connection pool and might even lead to dead locks when multiple methods request a fresh connection while holding on to one.
1014+
1015+
NOTE: It is definitely reasonable to mark read-only queries as such by setting the `readOnly` flag.
10121016
This does not, however, act as a check that you do not trigger a manipulating query (although some databases reject `INSERT` and `UPDATE` statements inside a read-only transaction).
10131017
Instead, the `readOnly` flag is propagated as a hint to the underlying JDBC driver for performance optimizations.
10141018

0 commit comments

Comments
 (0)