Skip to content

Commit b8f3291

Browse files
committed
Add documentation for streaming results.
Also us Github issue numbers on tests. See #578, #971
1 parent 1e17031 commit b8f3291

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

Diff for: spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/QueryAnnotationHsqlIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void executeCustomQueryWithReturnTypeIsStream() {
174174
.containsExactlyInAnyOrder("a", "b");
175175
}
176176

177-
@Test // DATAJDBC-356
177+
@Test // GH-578
178178
public void executeCustomQueryWithNamedParameterAndReturnTypeIsStream() {
179179

180180
repository.save(dummyEntity("a"));

Diff for: spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQueryUnitTests.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.data.repository.Repository;
4141
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
4242
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
43-
import org.springframework.data.repository.query.DefaultParameters;
4443
import org.springframework.jdbc.core.ResultSetExtractor;
4544
import org.springframework.jdbc.core.RowMapper;
4645
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
@@ -57,16 +56,15 @@
5756
* @author Mark Paluch
5857
* @author Dennis Effing
5958
*/
60-
public class StringBasedJdbcQueryUnitTests {
61-
59+
class StringBasedJdbcQueryUnitTests {
6260

6361
RowMapper<Object> defaultRowMapper;
6462
NamedParameterJdbcOperations operations;
6563
RelationalMappingContext context;
6664
JdbcConverter converter;
6765

6866
@BeforeEach
69-
public void setup() throws NoSuchMethodException {
67+
void setup() throws NoSuchMethodException {
7068

7169
this.defaultRowMapper = mock(RowMapper.class);
7270
this.operations = mock(NamedParameterJdbcOperations.class);
@@ -75,17 +73,16 @@ public void setup() throws NoSuchMethodException {
7573
}
7674

7775
@Test // DATAJDBC-165
78-
public void emptyQueryThrowsException() {
76+
void emptyQueryThrowsException() {
7977

8078
JdbcQueryMethod queryMethod = createMethod("noAnnotation");
8179

8280
Assertions.assertThatExceptionOfType(IllegalStateException.class) //
83-
.isThrownBy(() -> createQuery(queryMethod)
84-
.execute(new Object[] {}));
81+
.isThrownBy(() -> createQuery(queryMethod).execute(new Object[] {}));
8582
}
8683

8784
@Test // DATAJDBC-165
88-
public void defaultRowMapperIsUsedByDefault() {
85+
void defaultRowMapperIsUsedByDefault() {
8986

9087
JdbcQueryMethod queryMethod = createMethod("findAll");
9188
StringBasedJdbcQuery query = createQuery(queryMethod);
@@ -94,7 +91,7 @@ public void defaultRowMapperIsUsedByDefault() {
9491
}
9592

9693
@Test // DATAJDBC-165, DATAJDBC-318
97-
public void customRowMapperIsUsedWhenSpecified() {
94+
void customRowMapperIsUsedWhenSpecified() {
9895

9996
JdbcQueryMethod queryMethod = createMethod("findAllWithCustomRowMapper");
10097
StringBasedJdbcQuery query = createQuery(queryMethod);
@@ -103,7 +100,7 @@ public void customRowMapperIsUsedWhenSpecified() {
103100
}
104101

105102
@Test // DATAJDBC-290
106-
public void customResultSetExtractorIsUsedWhenSpecified() {
103+
void customResultSetExtractorIsUsedWhenSpecified() {
107104

108105
JdbcQueryMethod queryMethod = createMethod("findAllWithCustomResultSetExtractor");
109106
StringBasedJdbcQuery query = createQuery(queryMethod);
@@ -117,7 +114,7 @@ public void customResultSetExtractorIsUsedWhenSpecified() {
117114
}
118115

119116
@Test // DATAJDBC-290
120-
public void customResultSetExtractorAndRowMapperGetCombined() {
117+
void customResultSetExtractorAndRowMapperGetCombined() {
121118

122119
JdbcQueryMethod queryMethod = createMethod("findAllWithCustomRowMapperAndResultSetExtractor");
123120
StringBasedJdbcQuery query = createQuery(queryMethod);
@@ -131,8 +128,9 @@ public void customResultSetExtractorAndRowMapperGetCombined() {
131128
"RowMapper is not expected to be custom");
132129
}
133130

134-
@Test // DATAJDBC-356
135-
public void streamQueryCallsQueryForStreamOnOperations() {
131+
@Test // GH-578
132+
void streamQueryCallsQueryForStreamOnOperations() {
133+
136134
JdbcQueryMethod queryMethod = createMethod("findAllWithStreamReturnType");
137135
StringBasedJdbcQuery query = createQuery(queryMethod);
138136

@@ -141,20 +139,21 @@ public void streamQueryCallsQueryForStreamOnOperations() {
141139
verify(operations).queryForStream(eq("some sql statement"), any(SqlParameterSource.class), any(RowMapper.class));
142140
}
143141

144-
@Test // DATAJDBC-356
142+
@Test // GH-578
145143
void streamQueryFallsBackToCollectionQueryWhenCustomResultSetExtractorIsSpecified() {
144+
146145
JdbcQueryMethod queryMethod = createMethod("findAllWithStreamReturnTypeAndResultSetExtractor");
147146
StringBasedJdbcQuery query = createQuery(queryMethod);
148147

149148
query.execute(new Object[] {});
150149

151-
ArgumentCaptor<ResultSetExtractor> captor = ArgumentCaptor.forClass(ResultSetExtractor.class);
150+
ArgumentCaptor<ResultSetExtractor<?>> captor = ArgumentCaptor.forClass(ResultSetExtractor.class);
152151
verify(operations).query(eq("some sql statement"), any(SqlParameterSource.class), captor.capture());
153152
assertThat(captor.getValue()).isInstanceOf(CustomResultSetExtractor.class);
154153
}
155154

156155
@Test // GH-774
157-
public void sliceQueryNotSupported() {
156+
void sliceQueryNotSupported() {
158157

159158
JdbcQueryMethod queryMethod = createMethod("sliceAll", Pageable.class);
160159

@@ -164,7 +163,7 @@ public void sliceQueryNotSupported() {
164163
}
165164

166165
@Test // GH-774
167-
public void pageQueryNotSupported() {
166+
void pageQueryNotSupported() {
168167

169168
JdbcQueryMethod queryMethod = createMethod("pageAll", Pageable.class);
170169

Diff for: src/main/asciidoc/jdbc.adoc

+14
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ interface PersonRepository extends PagingAndSortingRepository<Person, String> {
466466
467467
@Query("SELECT * FROM person WHERE lastname = :lastname")
468468
List<Person> findByLastname(String lastname); <7>
469+
@Query("SELECT * FROM person WHERE lastname = :lastname")
470+
Stream<Person> streamByLastname(String lastname); <8>
469471
}
470472
----
471473
<1> The method shows a query for all people with the given `lastname`.
@@ -478,6 +480,7 @@ Thus, the method name results in a query expression of `SELECT … FROM person W
478480
It completes with `IncorrectResultSizeDataAccessException` on non-unique results.
479481
<6> In contrast to <3>, the first entity is always emitted even if the query yields more result documents.
480482
<7> The `findByLastname` method shows a query for all people with the given last name.
483+
<8> The `streamByLastname` method returns a `Stream` which makes values possible as soon as they are returned from the database.
481484
====
482485

483486
The following table shows the keywords that are supported for query methods:
@@ -622,6 +625,17 @@ Named queries are expected to be provided in the property file `META-INF/jdbc-na
622625

623626
The location of that file may be changed by setting a value to `@EnableJdbcRepositories.namedQueriesLocation`.
624627

628+
[[jdbc.query-methods.at-query.streaming-results]]
629+
==== Streaming Results
630+
631+
When you specify `Stream` as the return type of a query method Spring Data JDBC will return elements as soon as they become available.
632+
When dealing with large amounts of data this is suitable for reducing latency and memory requirements.
633+
634+
The stream contains an open connection to the database.
635+
To avoid memory leaks that connection needs to be closed eventually by closing the stream.
636+
The recommended way to do that is a try-with-resource clause.
637+
It also means once the connection to the database is closed, the stream cannot obtain further elements and will likely throw an exception.
638+
625639
[[jdbc.query-methods.at-query.custom-rowmapper]]
626640
==== Custom `RowMapper`
627641

Diff for: src/main/asciidoc/new-features.adoc

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33

44
This section covers the significant changes for each version.
55

6+
[[new-features.2-3-0]]
7+
== What's New in Spring Data JDBC 2.3
8+
9+
* Support for <<jdbc.query-methods.at-query.streaming-results, streaming results>>.
10+
* Support for specifying projection types as return type or using generics and providing a Class parameter to query methods.
11+
612
[[new-features.2-2-0]]
7-
== `Page` and `Slice` support for <<jdbc.query-methods,derived queries>>.
13+
== What's New in Spring Data JDBC 2.2
14+
* `Page` and `Slice` support for <<jdbc.query-methods,derived queries>>.
815

916
[[new-features.2-1-0]]
1017
== What's New in Spring Data JDBC 2.1

0 commit comments

Comments
 (0)