Description
It's very common to get first row of ResultSet or null
for empty ResultSet, for example JdbcStepExecutionDao::getLastStepExecution
from Spring Batch, see spring-projects/spring-batch#4798 for background.
We should call Statement::setMaxRows
as hints (some legacy driver may not honer it) before executing, and only consume first row of the ResultSet.
Not sure about the return type, @Nullable T
or Optional<T>
is better?
If this proposal is accepted, then Optional<T> first()
should be added to JdbcClient
.
-
Why not
queryForList
?
It will load all rows into memory. -
Why not
queryForObject
?
It requires exactly one row, but the ResultSet may be empty, and there is no standard syntax to limit one row across all databases. -
Why not
queryForStream().findFirst()
?
The result stream need be closed explicitly, andmaxRows
is set for wholeJdbcTemplate
, we need specific 1 only for this query.