Skip to content

Commit

Permalink
[CONJ-1124] ensure not having OOM when setting huge fetch size
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Nov 22, 2023
1 parent 7b8db20 commit 1562038
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/main/java/org/mariadb/jdbc/client/result/StreamingResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
* execute the next query. This can lead to OutOfMemoryError if not handled.
*/
public class StreamingResult extends Result {

private static final int MAX_FETCH_SIZE = 16384;
private final ReentrantLock lock;
private int dataFetchTime;
private int requestedFetchSize;

/**
* Constructor
Expand Down Expand Up @@ -87,8 +88,8 @@ public StreamingResult(
fetchSize);
this.lock = lock;
this.dataFetchTime = 0;
this.data = new byte[Math.max(fetchSize, 10)][];

this.requestedFetchSize = fetchSize;
this.data = new byte[Math.min(MAX_FETCH_SIZE, Math.max(fetchSize, 10))][];
addStreamingValue();
}

Expand Down Expand Up @@ -384,16 +385,22 @@ public boolean previous() throws SQLException {
@Override
public int getFetchSize() throws SQLException {
checkClose();
return super.getFetchSize();
return requestedFetchSize;
}

@Override
public void setFetchSize(int fetchSize) throws SQLException {
super.setFetchSize(fetchSize);
// ensure huge fetch size won't create OOM because of array size exceeding VM limit
// when using fetchSize with value different from 0, value must be small because goal is to
// ensure not having too
// much data in memory
// so fetch size when explicitly different from 0 is limited to 16K rows
super.setFetchSize(Math.min(MAX_FETCH_SIZE, fetchSize));
this.requestedFetchSize = fetchSize;
checkClose();
if (fetchSize == 0) {
// fetch all results
while (!loaded) {
while (!this.loaded) {
addStreamingValue();
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/test/java/org/mariadb/jdbc/integration/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,30 @@ public void testNegativeFetchSize() throws SQLException {
stmt.executeBatch();
try (ResultSet rs = stmt.getGeneratedKeys()) {
assertThrowsContains(
SQLSyntaxErrorException.class,
() -> rs.setFetchSize(-2),
"invalid fetch size -2");
SQLSyntaxErrorException.class, () -> rs.setFetchSize(-2), "invalid fetch size -2");
}
}
}

@Test
public void testHugeFetchSize() throws SQLException {
Assumptions.assumeTrue(isMariaDBServer());
try (PreparedStatement stmt =
sharedConn.prepareStatement(
"SELECT seq from seq_1_to_20000",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
stmt.setFetchSize(Integer.MAX_VALUE);
int i = 0;
try (ResultSet rs = stmt.executeQuery()) {
rs.setFetchSize(Integer.MAX_VALUE);
while (rs.next()) i++;
}
assertEquals(20000, i);
}
}

@Test
public void largeMaxRows() throws SQLException {
Statement stmt = sharedConn.createStatement();
Expand Down

0 comments on commit 1562038

Please sign in to comment.