-
Notifications
You must be signed in to change notification settings - Fork 35
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
Conversation
@@ -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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
creating StreamSuppliers
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove throws USE
There was a problem hiding this comment.
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>."); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
The current way we cache Streams can cause memory leaks, see #147. Therefore we suggest removing the
StreamSupplier
cache and instead allowing users to cacheStreamSupplier
if frequently reusing the same Stream source.