@@ -71,6 +71,9 @@ public <T> Stream<T> unsafeQuery(final CheckedFunction<Connection, PreparedState
71
71
try {
72
72
connection .setAutoCommit (true );
73
73
connection .close ();
74
+ if (isStreamFailed ) {
75
+ throw new RuntimeException (streamException );
76
+ }
74
77
} catch (final SQLException e ) {
75
78
throw new RuntimeException (e );
76
79
}
@@ -84,9 +87,9 @@ public <T> Stream<T> unsafeQuery(final CheckedFunction<Connection, PreparedState
84
87
* This method differs from {@link DefaultJdbcDatabase#toUnsafeStream} in that it takes a streaming
85
88
* config that adjusts the fetch size dynamically according to sampled row size.
86
89
*/
87
- protected static <T > Stream <T > toUnsafeStream (final ResultSet resultSet ,
88
- final CheckedFunction <ResultSet , T , SQLException > mapper ,
89
- final JdbcStreamingQueryConfig streamingConfig ) {
90
+ protected <T > Stream <T > toUnsafeStream (final ResultSet resultSet ,
91
+ final CheckedFunction <ResultSet , T , SQLException > mapper ,
92
+ final JdbcStreamingQueryConfig streamingConfig ) {
90
93
return StreamSupport .stream (new Spliterators .AbstractSpliterator <>(Long .MAX_VALUE , Spliterator .ORDERED ) {
91
94
92
95
@ Override
@@ -102,7 +105,11 @@ public boolean tryAdvance(final Consumer<? super T> action) {
102
105
return true ;
103
106
} catch (final SQLException e ) {
104
107
LOGGER .error ("SQLState: {}, Message: {}" , e .getSQLState (), e .getMessage ());
105
- throw new RuntimeException (e );
108
+ streamException = e ;
109
+ isStreamFailed = true ;
110
+ // throwing an exception in tryAdvance() method lead to the endless loop in Spliterator and stream
111
+ // will never close
112
+ return false ;
106
113
}
107
114
}
108
115
0 commit comments