Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/src/main/asciidoc/introduction/Tuning.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The default fetch size can be overridden for a given query by calling link:{doc-
[WARNING]
====
The Oracle JDBC driver defaults to a JDBC fetch size of 10.
You should _always_ set explicitly `hibernate.jdbc.fetch_size` if you're using Oracle, or, even better, specify the parameter `defaultRowPrefetch` in the JDBC connection URL.
You should _always_ set `hibernate.jdbc.fetch_size` explicitly if you're using Oracle, or, even better, specify the parameter `defaultRowPrefetch` in the JDBC connection URL.
====

[[statement-batching]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.hibernate.cfg.AgroalSettings.AGROAL_CONFIG_PREFIX;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess;

/**
Expand Down Expand Up @@ -81,7 +84,7 @@ private static String extractIsolationAsString(Map<String, Object> properties) {
final Integer isolation = ConnectionProviderInitiator.extractIsolation( properties );
return isolation != null ?
// Agroal resolves transaction isolation from the 'nice' name
ConnectionProviderInitiator.toIsolationNiceName( isolation )
toIsolationNiceName( isolation )
: null;
}

Expand Down Expand Up @@ -177,11 +180,12 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
: extractDriverNameFromMetadata(),
dialect.getVersion(),
Boolean.toString( acfc.autoCommit() ),
acfc.jdbcTransactionIsolation() != null
? ConnectionProviderInitiator.toIsolationNiceName( acfc.jdbcTransactionIsolation().level() )
: null,
acfc.jdbcTransactionIsolation() != null && acfc.jdbcTransactionIsolation().isDefined()
? toIsolationNiceName( acfc.jdbcTransactionIsolation().level() )
: toIsolationNiceName( getIsolation( agroalDataSource ) ),
acpc.minSize(),
acpc.maxSize()
acpc.maxSize(),
getFetchSize( agroalDataSource )
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
Expand Down Expand Up @@ -40,7 +39,12 @@
import static org.hibernate.cfg.C3p0Settings.C3P0_MAX_STATEMENTS;
import static org.hibernate.cfg.C3p0Settings.C3P0_MIN_SIZE;
import static org.hibernate.cfg.C3p0Settings.C3P0_TIMEOUT;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.extractIsolation;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.extractSetting;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.getConnectionProperties;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getFetchSize;
import static org.hibernate.engine.jdbc.connections.internal.DatabaseConnectionInfoImpl.getIsolation;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInteger;

Expand Down Expand Up @@ -149,23 +153,28 @@ public void configure(Map<String, Object> properties) {
// as soon as we obtain a new connection. This maybe isn't ideal,
// and it's not what we do with Agroal or Hikari.
autocommit = getBoolean( JdbcSettings.AUTOCOMMIT, properties ); // defaults to false
isolation = ConnectionProviderInitiator.extractIsolation( properties );
isolation = extractIsolation( properties );

final Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( properties );
final Map<String, Object> poolSettings = poolSettings( properties );
final Properties connectionProps = getConnectionProperties( properties );
final var poolSettings = poolSettings( properties );
dataSource = createDataSource( jdbcUrl, connectionProps, poolSettings );

final Integer fetchSize = getFetchSize( dataSource );
if ( isolation == null ) {
isolation = getIsolation( dataSource );
}
dbInfoProducer = dialect -> new DatabaseConnectionInfoImpl(
C3P0ConnectionProvider.class,
jdbcUrl,
jdbcDriverClass,
dialect.getVersion(),
Boolean.toString( autocommit ),
isolation == null ? null : ConnectionProviderInitiator.toIsolationNiceName( isolation ),
isolation == null ? null : toIsolationNiceName( isolation ),
requireNonNullElse( getInteger( C3P0_STYLE_MIN_POOL_SIZE.substring( 5 ), poolSettings ),
DEFAULT_MIN_POOL_SIZE ),
requireNonNullElse( getInteger( C3P0_STYLE_MAX_POOL_SIZE.substring( 5 ), poolSettings ),
DEFAULT_MAX_POOL_SIZE )
DEFAULT_MAX_POOL_SIZE ),
fetchSize
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
package org.hibernate.test.c3p0;

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.SybaseASEDialect;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;

import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.Test;
Expand Down Expand Up @@ -92,17 +88,4 @@ public void testHHH6635() throws Exception {

assertTrue( "PooledDataSource BMean not found, please verify version of c3p0", mbeanfound );
}

@Test @JiraKey(value="HHH-9498")
public void testIsolationPropertyCouldBeEmpty() {
C3P0ConnectionProvider provider = new C3P0ConnectionProvider();
try {
Map<String,Object> configuration = new HashMap<>();
configuration.put( Environment.ISOLATION, "" );
provider.configure( configuration );
}
finally {
provider.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,32 @@ public interface JdbcLogging extends BasicLogger {
@Message(value = "Unable to roll back isolated connection on exception ", id = 100021)
void unableToRollBackIsolatedConnection(@Cause Exception ignored);

@LogMessage(level = DEBUG)
@Message(value = "Using default JDBC fetch size: %s", id = 100022)
void usingFetchSize(int fetchSize);

@LogMessage(level = WARN)
@Message(value = "Low default JDBC fetch size: %s (consider setting 'hibernate.jdbc.fetch_size')", id = 100023)
void warnLowFetchSize(int fetchSize);

@LogMessage(level = TRACE)
@Message(value = "JDBC fetch size: %s", id = 100024)
void fetchSize(int fetchSize);

@LogMessage(level = DEBUG)
@Message(value = "Low JDBC fetch size: %s (consider setting 'hibernate.jdbc.fetch_size')", id = 100025)
void lowFetchSize(int fetchSize);

@LogMessage(level = TRACE)
@Message(value = "Setting JDBC fetch size: %s", id = 100026)
void settingFetchSize(int fetchSize);

@LogMessage(level = TRACE)
@Message(value = "Setting JDBC query timeout: %s", id = 100027)
void settingQueryTimeout(int timeout);

@LogMessage(level = WARN)
@Message(value = "Called joinTransaction() on a non-JTA EntityManager (ignoring)", id = 100025)
@Message(value = "Called joinTransaction() on a non-JTA EntityManager (ignoring)", id = 100030)
void callingJoinTransactionOnNonJtaEntityManager();

@LogMessage(level = TRACE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/
package org.hibernate.engine.jdbc.connections.internal;

import java.sql.SQLException;
import java.util.Map;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.dialect.DatabaseVersion;
import org.hibernate.dialect.Dialect;
Expand All @@ -14,6 +16,8 @@
import org.hibernate.internal.util.NullnessHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;

import javax.sql.DataSource;

import static org.hibernate.dialect.SimpleDatabaseVersion.ZERO_VERSION;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.interpretIsolation;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
Expand All @@ -37,6 +41,7 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
protected final String isolationLevel;
protected final Integer poolMinSize;
protected final Integer poolMaxSize;
private final Integer fetchSize;

public DatabaseConnectionInfoImpl(
Class<? extends ConnectionProvider> connectionProviderClass,
Expand All @@ -46,7 +51,8 @@ public DatabaseConnectionInfoImpl(
String autoCommitMode,
String isolationLevel,
Integer poolMinSize,
Integer poolMaxSize) {
Integer poolMaxSize,
Integer fetchSize) {
this.connectionProviderClass = connectionProviderClass;
this.jdbcUrl = nullIfEmpty( jdbcUrl );
this.jdbcDriver = nullIfEmpty( jdbcDriver );
Expand All @@ -55,6 +61,7 @@ public DatabaseConnectionInfoImpl(
this.isolationLevel = nullIfEmpty( isolationLevel );
this.poolMinSize = poolMinSize;
this.poolMaxSize = poolMaxSize;
this.fetchSize = fetchSize;
}

public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect) {
Expand All @@ -67,12 +74,33 @@ public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect)
determineIsolationLevel( settings ),
// No setting for min. pool size
null,
determinePoolMaxSize( settings )
determinePoolMaxSize( settings ),
null
);
}

public DatabaseConnectionInfoImpl(Dialect dialect) {
this( null, null, null, dialect.getVersion(), null, null, null, null );
this( null, null, null, dialect.getVersion(), null, null, null, null, null );
}

public static Integer getFetchSize(DataSource dataSource) {
try ( var conn = dataSource.getConnection() ) {
try ( var statement = conn.createStatement() ) {
return statement.getFetchSize();
}
}
catch ( SQLException ignored ) {
return null;
}
}

public static Integer getIsolation(DataSource dataSource) {
try ( var conn = dataSource.getConnection() ) {
return conn.getTransactionIsolation();
}
catch ( SQLException ignored ) {
return null;
}
}

@Override
Expand Down Expand Up @@ -110,16 +138,34 @@ public Integer getPoolMaxSize() {
return poolMaxSize;
}

@Override
public @Nullable Integer getJdbcFetchSize() {
return fetchSize;
}

@Override
public String toInfoString() {
return "\tDatabase JDBC URL [" + handleEmpty( jdbcUrl ) + ']' +
"\n\tDatabase driver: " + handleEmpty( jdbcDriver ) +
"\n\tDatabase version: " + handleEmpty( dialectVersion ) +
"\n\tAutocommit mode: " + handleEmpty( autoCommitMode ) +
"\n\tIsolation level: " + handleEmpty( isolationLevel ) +
"\n\tPool: " + handleEmpty( connectionProviderClass ) +
"\n\tMinimum pool size: " + handleEmpty( poolMinSize ) +
"\n\tMaximum pool size: " + handleEmpty( poolMaxSize );
return """
\tDatabase JDBC URL [%s]
\tDatabase driver: %s
\tDatabase version: %s
\tAutocommit mode: %s
\tIsolation level: %s
\tJDBC fetch size: %s
\tPool: %s
\tMinimum pool size: %s
\tMaximum pool size: %s"""
.formatted(
handleEmpty( jdbcUrl ),
handleEmpty( jdbcDriver ),
handleEmpty( dialectVersion ),
handleEmpty( autoCommitMode ),
handleEmpty( isolationLevel ),
handleEmpty( fetchSize ),
handleEmpty( connectionProviderClass ),
handleEmpty( poolMinSize ),
handleEmpty( poolMaxSize )
);
}

private static String handleEmpty(String value) {
Expand All @@ -131,6 +177,10 @@ private static String handleEmpty(DatabaseVersion dialectVersion) {
}

private static String handleEmpty(Integer value) {
return value != null ? ( value == 0 ? "none" : value.toString() ) : DEFAULT;
}

private static String handleFetchSize(Integer value) {
return value != null ? value.toString() : DEFAULT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,21 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, Extract
final String url;
final String driver;
final String isolationLevel;
final Integer fetchSize;
if ( metaData != null ) {
url = metaData.getUrl();
driver = metaData.getDriver();
isolationLevel = toIsolationNiceName( metaData.getTransactionIsolation() )
isolationLevel =
toIsolationNiceName( metaData.getTransactionIsolation() )
+ " [default " + toIsolationNiceName( metaData.getDefaultTransactionIsolation() ) + "]";
final int defaultFetchSize = metaData.getDefaultFetchSize();
fetchSize = defaultFetchSize == -1 ? null : defaultFetchSize;
}
else {
url = null;
driver = null;
isolationLevel = null;
fetchSize = null;
}

return new DatabaseConnectionInfoImpl(
Expand All @@ -180,7 +185,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, Extract
null,
isolationLevel,
null,
null
null,
fetchSize
) {
@Override
public String toInfoString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.hibernate.internal.log.ConnectionInfoLogger;

import static org.hibernate.cfg.JdbcSettings.JAKARTA_JDBC_URL;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.extractIsolation;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.getConnectionProperties;
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.toIsolationNiceName;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
import static org.hibernate.internal.util.config.ConfigurationHelper.getInt;
Expand Down Expand Up @@ -134,10 +136,10 @@ private static ConnectionCreator buildCreator(

final String driverList = success ? driverClassName : driverList();

final Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties( configurationValues );
final Properties connectionProps = getConnectionProperties( configurationValues );

final boolean autoCommit = getBoolean( AvailableSettings.AUTOCOMMIT, configurationValues );
final Integer isolation = ConnectionProviderInitiator.extractIsolation( configurationValues );
final Integer isolation = extractIsolation( configurationValues );
final String initSql = (String) configurationValues.get( INIT_SQL );

final ConnectionCreatorFactory factory = getConnectionCreatorFactory( configurationValues, serviceRegistry );
Expand All @@ -150,7 +152,8 @@ private static ConnectionCreator buildCreator(
Boolean.toString( autoCommit ),
isolation != null ? toIsolationNiceName( isolation ) : null,
getInt( MIN_SIZE, configurationValues, 1 ),
getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 )
getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 ),
null
);

return factory.create(
Expand Down Expand Up @@ -292,7 +295,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
dbInfo.getAutoCommitMode(),
dbInfo.getIsolationLevel(),
dbInfo.getPoolMinSize(),
dbInfo.getPoolMaxSize()
dbInfo.getPoolMaxSize(),
dbInfo.getJdbcFetchSize()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
null,
null,
null,
null,
null
) {
@Override
Expand Down
Loading