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

Discard Stream cache and expose StreamSupplier #342

Merged
merged 12 commits into from
Jun 19, 2023
Merged

Conversation

julgus
Copy link
Member

@julgus julgus commented Jun 16, 2023

The current way we cache Streams can cause memory leaks, see #147. Therefore we suggest removing the StreamSupplier cache and instead allowing users to cache StreamSupplier if frequently reusing the same Stream source.

@@ -25,7 +25,10 @@
import java.util.stream.Stream;

/**
* A JPAStreamer is responsible for creating Streams from data sources
* A JPAStreamer is responsible for creating Streams from data sources,
* alternatively for creating Streamers that can be reused to create Streams of the same Entity source,
Copy link
Contributor

Choose a reason for hiding this comment

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

creating StreamSuppliers

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, a relic of my first attempt to introduce Streamers.

* If you are using the same Stream source frequently e.g. Film.class,
* consider configuring a {@link StreamSupplier} that can supply
* {@link Stream}s from the same source over and over again.
* This save resources and avoids instantiating a new {@link EntityManager} for each new {@link Stream}s.
Copy link
Contributor

@minborg minborg Jun 16, 2023

Choose a reason for hiding this comment

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

for each new Streams (singularis)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

* <p>
* Here is an example of using a {@link StreamSupplier}:
* <pre>{@code
* final StreamSupplier<Film> streamSupplier = jpaStreamer.createStreamSupplier(Film.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using Try-With-Resources would be better here.

Copy link
Member Author

@julgus julgus Jun 16, 2023

Choose a reason for hiding this comment

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

Could I provide both examples? I just think this way is clearer, but I totally get that TWR is a better practice.

* @author Per Minborg, Julia Gustafsson
* @since 3.0.1
*/
public interface StreamSupplier<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

add implements AutoCloseable allowing TWR use.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean extends AutoCloseable?

@@ -33,7 +34,7 @@ final class StandardJPAStreamer implements JPAStreamer {

private final Supplier<EntityManager> entityManagerSupplier;
private final Runnable closeHandler;
private final Map<StreamConfiguration<?>, Streamer<?>> streamerCache;
private final Map<StreamConfiguration<?>, StreamSupplier<?>> streamerCache;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think now we can delete this field.

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right!

}

@Override
public void resetStreamer(Class<?>... entityClasses) throws UnsupportedOperationException {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not declare throws UOE

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

}

@Override
public void close() {
streamerCache.values().forEach(Streamer::close);
streamerCache.values().forEach(StreamSupplier::close);
Copy link
Contributor

Choose a reason for hiding this comment

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

This line can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

public void close() {
//System.out.println("Closing Streamer<" + entityClass.getSimpleName() + ">");
renderer.close();
public void close() throws UnsupportedOperationException {
Copy link
Contributor

Choose a reason for hiding this comment

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

remove throws USE

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

if (this.closeEntityManager) {
renderer.close();
} else {
System.out.println("JPAStreamer does not close Entity Managers obtained by a given Supplier<EntityManager>.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should only print this message once. So we could have a static field of type AtomicBoolean that controls this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we need to discuss this briefly. This slight complexity arose due to compatibility with Quarkus.

@julgus julgus merged commit 1a92f6a into develop Jun 19, 2023
@julgus julgus deleted the stream-supplier branch June 19, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants