Skip to content

Commit

Permalink
possible mitigation for Oracle fetch size issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 16, 2025
1 parent afd2628 commit 7a2345f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
private final String url;
private final String driver;
private final boolean jdbcMetadataAccessible;

private final int defaultFetchSize;

//Lazily initialized: loading all sequence information upfront has been
//shown to be too slow in some cases. In this way we only load it
Expand All @@ -71,6 +72,7 @@ private ExtractedDatabaseMetaDataImpl(
SQLStateType sqlStateType,
int transactionIsolation,
int defaultTransactionIsolation,
int defaultFetchSize,
String url,
String driver,
boolean jdbcMetadataIsAccessible) {
Expand All @@ -88,6 +90,7 @@ private ExtractedDatabaseMetaDataImpl(
this.sqlStateType = sqlStateType;
this.transactionIsolation = transactionIsolation;
this.defaultTransactionIsolation = defaultTransactionIsolation;
this.defaultFetchSize = defaultFetchSize;
this.url = url;
this.driver = driver;
this.jdbcMetadataAccessible = jdbcMetadataIsAccessible;
Expand Down Expand Up @@ -168,6 +171,11 @@ public int getDefaultTransactionIsolation() {
return defaultTransactionIsolation;
}

@Override
public int getDefaultFetchSize() {
return defaultFetchSize;
}

@Override
public synchronized List<SequenceInformation> getSequenceInformationList() {
if ( jdbcMetadataAccessible ) {
Expand Down Expand Up @@ -211,6 +219,7 @@ public static class Builder {
private String driver;
private int defaultTransactionIsolation;
private int transactionIsolation;
private int defaultFetchSize;

public Builder(JdbcEnvironment jdbcEnvironment, boolean jdbcMetadataIsAccessible, JdbcConnectionAccess connectionAccess) {
this.jdbcEnvironment = jdbcEnvironment;
Expand All @@ -233,6 +242,12 @@ public Builder apply(DatabaseMetaData databaseMetaData) throws SQLException {
driver = databaseMetaData.getDriverName();
defaultTransactionIsolation = databaseMetaData.getDefaultTransactionIsolation();
transactionIsolation = databaseMetaData.getConnection().getTransactionIsolation();
try ( Statement statement = databaseMetaData.getConnection().createStatement() ) {
defaultFetchSize = statement.getFetchSize();
}
catch (SQLException sqle) {
defaultFetchSize = -1;
}
return this;
}

Expand Down Expand Up @@ -302,6 +317,7 @@ public ExtractedDatabaseMetaDataImpl build() {
sqlStateType,
transactionIsolation,
defaultTransactionIsolation,
defaultFetchSize,
url,
driver,
jdbcMetadataIsAccessible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
Expand Down Expand Up @@ -103,6 +104,8 @@ public JdbcEnvironmentImpl(final ServiceRegistryImplementor serviceRegistry, fin
this.qualifiedObjectNameFormatter = new QualifiedObjectNameFormatterStandardImpl( nameQualifierSupport );

this.lobCreatorBuilder = makeLobCreatorBuilder( dialect );

logJdbcFetchSize( extractedMetaDataSupport.getDefaultFetchSize(), cfgService );
}

private IdentifierHelperBuilder identifierHelperBuilder(
Expand Down Expand Up @@ -312,6 +315,8 @@ public JdbcEnvironmentImpl(
cfgService.getSettings(),
databaseMetaData.getConnection()
);

logJdbcFetchSize( extractedMetaDataSupport.getDefaultFetchSize(), cfgService );
}

private static IdentifierHelper identifierHelper(
Expand Down Expand Up @@ -420,4 +425,14 @@ public LobCreatorBuilder getLobCreatorBuilder() {
return lobCreatorBuilder;
}

private static void logJdbcFetchSize(int defaultFetchSize, ConfigurationService cfgService) {
if ( !cfgService.getSettings().containsKey( JdbcSettings.STATEMENT_FETCH_SIZE ) ) {
if ( defaultFetchSize > 0 && defaultFetchSize < 200 ) {
log.warn( "Low default JDBC fetch size: " + defaultFetchSize + " (set hibernate.jdbc.fetch_size)" );
}
else {
log.debug( "Using default JDBC fetch size: " + defaultFetchSize );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public interface ExtractedDatabaseMetaData {
*/
int getDefaultTransactionIsolation();

/**
* Retrieve the default JDBC {@link java.sql.Statement#getFetchSize fetch size}.
*/
int getDefaultFetchSize();

/**
* Retrieve the list of {@code SequenceInformation} objects which describe the underlying database sequences.
*
Expand Down

0 comments on commit 7a2345f

Please sign in to comment.