Skip to content

🐛 Fixed connection leak in StreamingJdbcDatabase #20888

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

Merged
merged 5 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
public abstract class JdbcDatabase extends SqlDatabase {

protected final JdbcCompatibleSourceOperations<?> sourceOperations;
protected Exception streamException;
protected boolean isStreamFailed;

public JdbcDatabase(final JdbcCompatibleSourceOperations<?> sourceOperations) {
this.sourceOperations = sourceOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public <T> Stream<T> unsafeQuery(final CheckedFunction<Connection, PreparedState
try {
connection.setAutoCommit(true);
connection.close();
if (isStreamFailed) {
throw new RuntimeException(streamException);
}
} catch (final SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -84,7 +87,7 @@ public <T> Stream<T> unsafeQuery(final CheckedFunction<Connection, PreparedState
* This method differs from {@link DefaultJdbcDatabase#toUnsafeStream} in that it takes a streaming
* config that adjusts the fetch size dynamically according to sampled row size.
*/
protected static <T> Stream<T> toUnsafeStream(final ResultSet resultSet,
protected <T> Stream<T> toUnsafeStream(final ResultSet resultSet,
final CheckedFunction<ResultSet, T, SQLException> mapper,
final JdbcStreamingQueryConfig streamingConfig) {
return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED) {
Expand All @@ -102,7 +105,10 @@ public boolean tryAdvance(final Consumer<? super T> action) {
return true;
} catch (final SQLException e) {
LOGGER.error("SQLState: {}, Message: {}", e.getSQLState(), e.getMessage());
throw new RuntimeException(e);
streamException = e;
isStreamFailed = true;
// throwing an exception in tryAdvance() method lead to the endless loop in Spliterator and stream will never close
return false;
}
}

Expand Down