Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataAccessUtils result accessors with Optional return value #27735

Merged
merged 11 commits into from
Jul 14, 2023

Conversation

KaidosGH
Copy link
Contributor

@KaidosGH KaidosGH commented Nov 26, 2021

Closes #27728
(#27728)

Hi everyone, this is my first PR in any project here on github with the goal of improving my understanding of java. Hope i understood the enhancement request correctly.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 26, 2021
@KaidosGH KaidosGH changed the title Feature/extend data access utils extend DataAccessUtils Nov 26, 2021
@BrucessKING
Copy link

BrucessKING commented Nov 26, 2021

Using the * form of import should be avoided - java.util.*.

@rstoyanchev rstoyanchev added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement labels Nov 26, 2021
@rstoyanchev rstoyanchev changed the title extend DataAccessUtils DataAccessUtils result accessors with Optional return value Nov 26, 2021
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 26, 2021
@rstoyanchev rstoyanchev added this to the 5.3.14 milestone Nov 26, 2021
@jhoeller jhoeller modified the milestones: 5.3.14, 6.0 M1 Nov 26, 2021
if (results == null) {
return null;
}
List<T> resultList = results.toList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to do a

.limit(2).toList()

and then avoid opening the second stream and instead do

resultList.get(0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @marschall,

thanks for reviewing, sounds good, i refactored this. Does it make more sense now?

throw new IncorrectResultSizeDataAccessException(1, resultList.size());
as we limited the resultList size with .limit, this would not report the original passed Stream results size though but instead a maximum of 2... is that an issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks better to me.

return null;
}
Iterable<T> iterable = () -> results;
List<T> resultList = StreamSupport.stream(iterable.spliterator(), false).toList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a stream instead of just

results.next()

@KaidosGH
Copy link
Contributor Author

Using the * form of import should be avoided - java.util.*.

Thanks for the tip, i adjusted that.

if (results == null) {
return null;
}
List<T> resultList = results.limit(2).toList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a trade off here and in #optionalResult whether we should call #close on the stream:

On one hand Java convention is the code who creates a "resource" is responsible for disposing it, in this case the caller of this method would be responsible to call #close on the streams where required.
On the other hand failing to call #close on some streams, like the ones returned by JdbcOperations#queryForStream, can lead to resource leaks. Having the caller put streams in a try-with-resources block is both cumbersome and error prone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The passed stream is now closed in both methods. Also e.g. hibernate's Query.stream() documentation suggests the stream should be closed afterwards, so not so far fetched 👍

Copy link

@rkvigneswaran rkvigneswaran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

This is my first review comments in any open source project. Kindly let me know if my comments are invalid.

KaidosGH and others added 4 commits November 28, 2021 04:02
…cessUtils.java


re-use newly added singleResult methods

Co-authored-by: Vigneswaran <[email protected]>
…cessUtils.java


re-use newly added singleResult methods

Co-authored-by: Vigneswaran <[email protected]>
…cessUtils.java


re-use newly added singleResult methods

Co-authored-by: Vigneswaran <[email protected]>
Copy link

@rkvigneswaran rkvigneswaran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying the exception to make it clear that size of the underlying Stream/Iterator is not known

KaidosGH and others added 3 commits November 28, 2021 15:10
…cessUtils.java


use "public IncorrectResultSizeDataAccessException(int expectedSize)" in stream and iterator method

Co-authored-by: Kwangyong Kim <[email protected]>
…cessUtils.java


use "public IncorrectResultSizeDataAccessException(int expectedSize)" in stream and iterator method

Co-authored-by: Vigneswaran <[email protected]>
@jhoeller jhoeller modified the milestones: 6.0 M1, 6.0 M2 Dec 13, 2021
@jhoeller jhoeller modified the milestones: 6.0.0-M2, 6.0.0-M3 Jan 11, 2022
@jhoeller jhoeller removed their assignment Jan 24, 2022
@jhoeller jhoeller modified the milestones: 6.0.0-M3, 6.0.x Mar 15, 2022
@jhoeller jhoeller modified the milestones: 6.0.x, 6.x Backlog Jan 11, 2023
@jhoeller jhoeller self-assigned this Jul 13, 2023
@jhoeller jhoeller modified the milestones: 6.x Backlog, 6.1.0-M3 Jul 13, 2023
@jhoeller jhoeller merged commit 4b80b0f into spring-projects:main Jul 14, 2023
jhoeller added a commit that referenced this pull request Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataAccessUtils result accessors with Optional return value
8 participants