Skip to content

Commit 6a9bc9b

Browse files
committed
logging for JDBC fetch size and query timeout
1 parent 8b7947d commit 6a9bc9b

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/JdbcLogging.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,24 @@ public interface JdbcLogging extends BasicLogger {
152152
@Message(value = "Unable to roll back isolated connection on exception ", id = 100021)
153153
void unableToRollBackIsolatedConnection(@Cause Exception ignored);
154154

155+
@LogMessage(level = TRACE)
156+
@Message(value = "JDBC fetch size: %s", id = 100022)
157+
void fetchSize(int fetchSize);
158+
159+
@LogMessage(level = DEBUG)
160+
@Message(value = "Low JDBC fetch size: %s (consider setting 'hibernate.jdbc.fetch_size')", id = 100023)
161+
void lowFetchSize(int fetchSize);
162+
163+
@LogMessage(level = TRACE)
164+
@Message(value = "Setting JDBC fetch size: %s", id = 100024)
165+
void settingFetchSize(int fetchSize);
166+
167+
@LogMessage(level = TRACE)
168+
@Message(value = "Setting JDBC query timeout: %s", id = 100025)
169+
void settingQueryTimeout(int timeout);
170+
155171
@LogMessage(level = WARN)
156-
@Message(value = "Called joinTransaction() on a non-JTA EntityManager (ignoring)", id = 100025)
172+
@Message(value = "Called joinTransaction() on a non-JTA EntityManager (ignoring)", id = 100026)
157173
void callingJoinTransactionOnNonJtaEntityManager();
158174

159175
@LogMessage(level = TRACE)

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import org.checkerframework.checker.nullness.qual.Nullable;
2727

28+
import static org.hibernate.engine.jdbc.JdbcLogging.JDBC_MESSAGE_LOGGER;
29+
2830
/**
2931
* Standard implementation of {@link StatementPreparer}.
3032
*
@@ -141,7 +143,7 @@ public PreparedStatement prepareQueryStatement(
141143
final int resultSetType;
142144
if ( scrollMode != null && scrollMode != ScrollMode.FORWARD_ONLY ) {
143145
if ( !settings().isScrollableResultSetsEnabled() ) {
144-
throw new AssertionFailure("scrollable result sets are not enabled");
146+
throw new AssertionFailure( "Scrollable result sets are not enabled" );
145147
}
146148
resultSetType = scrollMode.toResultSetType();
147149
}
@@ -224,8 +226,21 @@ public void postProcess(PreparedStatement preparedStatement) throws SQLException
224226
}
225227

226228
private void setStatementFetchSize(PreparedStatement statement) throws SQLException {
227-
if ( settings().getFetchSizeOrNull() != null ) {
228-
statement.setFetchSize( settings().getFetchSizeOrNull() );
229+
final Integer fetchSize = settings().getFetchSizeOrNull();
230+
if ( fetchSize != null ) {
231+
JDBC_MESSAGE_LOGGER.settingFetchSize( fetchSize );
232+
statement.setFetchSize( fetchSize );
233+
}
234+
else {
235+
if ( JDBC_MESSAGE_LOGGER.isDebugEnabled() ) {
236+
final int defaultFetchSize = statement.getFetchSize();
237+
if ( defaultFetchSize < 100 ) {
238+
JDBC_MESSAGE_LOGGER.lowFetchSize( statement.getFetchSize() );
239+
}
240+
}
241+
else if ( JDBC_MESSAGE_LOGGER.isTraceEnabled() ) {
242+
JDBC_MESSAGE_LOGGER.fetchSize( statement.getFetchSize() );
243+
}
229244
}
230245
}
231246

hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/DeferredResultSetAccess.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.hibernate.sql.exec.spi.JdbcSelectExecutor;
3737

3838
import static java.util.Collections.emptyMap;
39+
import static org.hibernate.engine.jdbc.JdbcLogging.JDBC_MESSAGE_LOGGER;
3940

4041
/**
4142
* @author Steve Ebersole
@@ -241,11 +242,15 @@ private void setQueryOptions(PreparedStatement preparedStatement) throws SQLExce
241242
final QueryOptions queryOptions = executionContext.getQueryOptions();
242243
// set options
243244
if ( queryOptions != null ) {
244-
if ( queryOptions.getFetchSize() != null ) {
245-
preparedStatement.setFetchSize( queryOptions.getFetchSize() );
245+
final Integer fetchSize = queryOptions.getFetchSize();
246+
if ( fetchSize != null ) {
247+
JDBC_MESSAGE_LOGGER.settingFetchSize( fetchSize );
248+
preparedStatement.setFetchSize( fetchSize );
246249
}
247-
if ( queryOptions.getTimeout() != null ) {
248-
preparedStatement.setQueryTimeout( queryOptions.getTimeout() );
250+
final Integer timeout = queryOptions.getTimeout();
251+
if ( timeout != null ) {
252+
JDBC_MESSAGE_LOGGER.settingQueryTimeout( timeout );
253+
preparedStatement.setQueryTimeout( timeout );
249254
}
250255
}
251256
}

0 commit comments

Comments
 (0)