From 6404704311c572a927f7ad2b6ef494893523b95b Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 20 Jun 2022 11:27:34 +0200 Subject: [PATCH] HHH-15352 add ScrollableResults.setFetchSize() --- .../main/java/org/hibernate/ScrollableResults.java | 12 ++++++++++++ .../java/org/hibernate/cfg/AvailableSettings.java | 3 ++- .../internal/AbstractScrollableResults.java | 5 +++++ .../hibernate/internal/EmptyScrollableResults.java | 3 +++ .../results/jdbc/internal/JdbcValuesCacheHit.java | 3 +++ .../jdbc/internal/JdbcValuesResultSetImpl.java | 10 ++++++++++ .../hibernate/sql/results/jdbc/spi/JdbcValues.java | 2 ++ 7 files changed, 37 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/ScrollableResults.java b/hibernate-core/src/main/java/org/hibernate/ScrollableResults.java index 56f492f4f4..d84f068189 100644 --- a/hibernate-core/src/main/java/org/hibernate/ScrollableResults.java +++ b/hibernate-core/src/main/java/org/hibernate/ScrollableResults.java @@ -135,4 +135,16 @@ public interface ScrollableResults extends AutoCloseable, Closeable { * @return true if there is a row at that row number */ boolean setRowNumber(int rowNumber); + + /** + * Gives the JDBC driver a hint as to the number of rows that + * should be fetched from the database when more rows are needed. + * If {@code 0}, the JDBC driver's default setting will be used. + * + * @see java.sql.ResultSet#setFetchSize(int) + * @see org.hibernate.cfg.AvailableSettings#STATEMENT_FETCH_SIZE + * + * @since 6.1.1 + */ + void setFetchSize(int fetchSize); } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 7111ce0729..ebd63c7d7f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -945,8 +945,9 @@ public interface AvailableSettings { * from the database when more rows are needed. If {@code 0}, the JDBC driver's * default settings will be used. * - * @see java.sql.ResultSet#setFetchSize(int) + * @see java.sql.PreparedStatement#setFetchSize(int) * @see org.hibernate.boot.SessionFactoryBuilder#applyJdbcFetchSize(int) + * @see org.hibernate.ScrollableResults#setFetchSize(int) */ String STATEMENT_FETCH_SIZE = "hibernate.jdbc.fetch_size"; diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractScrollableResults.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractScrollableResults.java index e137db37c6..099fc9f524 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractScrollableResults.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractScrollableResults.java @@ -85,6 +85,11 @@ public abstract class AbstractScrollableResults implements ScrollableResultsI getPersistenceContext().afterScrollOperation(); } + @Override + public void setFetchSize(int fetchSize) { + getJdbcValues().setFetchSize(fetchSize); + } + @Override public final void close() { if ( this.closed ) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/EmptyScrollableResults.java b/hibernate-core/src/main/java/org/hibernate/internal/EmptyScrollableResults.java index f2cd6286e9..addb1f2ddc 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/EmptyScrollableResults.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/EmptyScrollableResults.java @@ -91,6 +91,9 @@ public class EmptyScrollableResults implements ScrollableResultsImplementor { return false; } + @Override + public void setFetchSize(int fetchSize) {} + @Override public Object[] get() { return ArrayHelper.EMPTY_OBJECT_ARRAY; diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java index 38eace844e..65325455e8 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java @@ -227,4 +227,7 @@ public class JdbcValuesCacheHit extends AbstractJdbcValues { protected void release() { cachedData = null; } + + @Override + public void setFetchSize(int fetchSize) {} } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java index 274e491e5a..7b014a4829 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java @@ -324,4 +324,14 @@ public class JdbcValuesResultSetImpl extends AbstractJdbcValues { public Object[] getCurrentRowValuesArray() { return currentRowJdbcValues; } + + @Override + public void setFetchSize(int fetchSize) { + try { + resultSetAccess.getResultSet().setFetchSize(fetchSize); + } + catch (SQLException e) { + throw makeExecutionException( "Error calling ResultSet.setFetchSize()", e ); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValues.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValues.java index b12649eddd..9f1dbcb382 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValues.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/spi/JdbcValues.java @@ -78,4 +78,6 @@ public interface JdbcValues { * Give implementations a chance to finish processing */ void finishUp(SharedSessionContractImplementor session); + + void setFetchSize(int fetchSize); }