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

Introduce ResultQueryFetchStream Refaster rule #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maxsumrall
Copy link
Member

When using the jOOQ SQL library for querying a database, you can either fetch the result set of your query into memory and proceed with application code, or you can return a lazy stream from the database and process the results as a stream. See here for examples and explanations of fetching with jOOQ. A common issue with the .stream() approach is that it is usually used unintentionally. This is dangerous as the stream returned here is not managed and not auto-closed, leaving applications potentially at risk to resource leaking.

A less opinionated version of this rule may be to only make this replacement when the .stream() is invoked outside of a try_with_resources block.

@maxsumrall maxsumrall force-pushed the maxsumrall/resultQueryFetchStream branch from 1489ec5 to 56ed83b Compare February 16, 2023 18:00
@maxsumrall
Copy link
Member Author

rebased on master

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.16.14</version>
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the latest version of jOOQ supporting Java 11. From 3.17.x, it is not supported.

@@ -1178,6 +1183,8 @@
| BSD 3-clause
| BSD License 3
| Eclipse Distribution License (New BSD License)
| Eclipse Distribution License - v 1.0
Copy link
Member Author

@maxsumrall maxsumrall Feb 16, 2023

Choose a reason for hiding this comment

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

Source:

[WARNING] License: 'Eclipse Distribution License - v 1.0' used by 1 dependencies:
 -Jakarta XML Binding API (jakarta.xml.bind:jakarta.xml.bind-api:3.0.0 - https://github.com/eclipse-ee4j/jaxb-api/jakarta.xml.bind-api)

@@ -1178,6 +1183,8 @@
| BSD 3-clause
| BSD License 3
| Eclipse Distribution License (New BSD License)
| Eclipse Distribution License - v 1.0
| EDL 1.0
Copy link
Member Author

Choose a reason for hiding this comment

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

Source:

[WARNING] License: 'EDL 1.0' used by 1 dependencies:
 -Jakarta Activation (com.sun.activation:jakarta.activation:2.0.0 - https://github.com/eclipse-ee4j/jaf/jakarta.activation)

Copy link
Contributor

@ferdinand-swoboda ferdinand-swoboda left a comment

Choose a reason for hiding this comment

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

Very useful rule!

We can apply the same to the synonym fetchStream() and almost the same for the equally dangerous fetchStreamInto(...) variants.

A less opinionated version of this rule may be to only make this replacement when the .stream() is invoked outside of a try_with_resources block.

That would certainly reduce false positives 👍🏼


/** Prefer eagerly fetching data over (lazy) streaming to avoid potentially leaking resources. */
@Description(
"Prefer eagerly fetching data over (lazy) streaming to avoid potentially leaking resources.")
Copy link
Contributor

@ferdinand-swoboda ferdinand-swoboda Feb 17, 2023

Choose a reason for hiding this comment

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

Would it be useful to link to the Jooq blogpost here?

Copy link
Member Author

Choose a reason for hiding this comment

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

While it would be useful, it might not be wise as the blogpost might move URLs one day, and then the code is not correct. I think that would not be prudent in a library such as this. Perhaps linking to it in this PR is enough for curious folks to find one day if they go looking for the motivations for this rule.

@rickie
Copy link
Member

rickie commented Feb 17, 2023

A less opinionated version of this rule may be to only make this replacement when the .stream() is invoked outside of a try_with_resources block.

That would certainly reduce false positives 👍🏼

If that reduces the false positive rate I think it'd be smart to introduce a Matcher for that. The Matcher can literally check for the try_with_resources block. There are some examples here. And of course, we can give extra pointers or discuss it if you're up for it.

@maxsumrall
Copy link
Member Author

A less opinionated version of this rule may be to only make this replacement when the .stream() is invoked outside of a try_with_resources block.

That would certainly reduce false positives 👍🏼

If that reduces the false positive rate I think it'd be smart to introduce a Matcher for that. The Matcher can literally check for the try_with_resources block. There are some examples here. And of course, we can give extra pointers or discuss it if you're up for it.

Thanks for the tip! I'll give it a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants