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
20 changes: 8 additions & 12 deletions documentation/src/main/asciidoc/introduction/Introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,19 @@ dependencies {
// the GOAT ORM
implementation 'org.hibernate.orm:hibernate-core:{fullVersion}'

// Hibernate Processor
annotationProcessor 'org.hibernate.orm:hibernate-processor:{fullVersion}'

// Hibernate Validator
implementation 'org.hibernate.validator:hibernate-validator:8.0.1.Final'
implementation 'org.glassfish:jakarta.el:4.0.2'

// Agroal connection pool
implementation 'org.hibernate.orm:hibernate-agroal:{fullVersion}'
implementation 'io.agroal:agroal-pool:2.1'
runtimeOnly 'org.hibernate.orm:hibernate-agroal:{fullVersion}'
runtimeOnly 'io.agroal:agroal-pool:2.5'

// logging via Log4j
implementation 'org.apache.logging.log4j:log4j-core:2.24.1'

// Hibernate Processor
annotationProcessor 'org.hibernate.orm:hibernate-processor:{fullVersion}'

// Compile-time checking for HQL
//implementation 'org.hibernate:query-validator:2.0-SNAPSHOT'
//annotationProcessor 'org.hibernate:query-validator:2.0-SNAPSHOT'
runtimeOnly 'org.apache.logging.log4j:log4j-core:2.24.1'

// H2 database
runtimeOnly 'com.h2database:h2:2.3.232'
Expand Down Expand Up @@ -281,8 +277,8 @@ public class Main {
// use H2 in-memory database
.jdbcUrl("jdbc:h2:mem:db1")
.jdbcCredentials("sa", "")
// use Agroal connection pool
.property("hibernate.agroal.maxSize", 20)
// set the Agroal connection pool size
.jdbcPoolSize(16)
// display SQL in console
.showSql(true, true, true)
.createEntityManagerFactory();
Expand Down
7 changes: 4 additions & 3 deletions documentation/src/main/asciidoc/introduction/Tuning.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ the connection pool.
The connection pool built in to Hibernate is suitable for testing, but isn't intended for use in production.
Instead, Hibernate supports several different connection pools, including our favorite, Agroal.

To select and configure Agroal, you'll need to set some extra configuration properties, in addition to the settings we already saw in <<basic-configuration-settings>>.
Hibernate will automatically make use of `AgroalConnectionProvider` if the module `org.hibernate.orm:hibernate-agroal` is available at runtime.

To properly configure Agroal, you'll need to set some extra configuration properties, in addition to the settings we already saw in <<basic-configuration-settings>>.
Properties with the prefix `hibernate.agroal` are passed through to Agroal:

[source,properties]
Expand All @@ -47,7 +49,6 @@ hibernate.agroal.acquisitionTimeout PT1s
hibernate.agroal.reapTimeout PT10s
----

As long as you set at least one property with the prefix `hibernate.agroal`, the `AgroalConnectionProvider` will be selected automatically.
There are many to choose from, as enumerated by link:{doc-javadoc-url}/org/hibernate/cfg/AgroalSettings.html[`AgroalSettings`]:

.Settings for configuring Agroal
Expand Down Expand Up @@ -78,7 +79,7 @@ The following settings are common to all connection pools supported by Hibernate
|===

A popular alternative to Agroal is HikariCP.
Its setting are enumerated by link:{doc-javadoc-url}/org/hibernate/cfg/HikariCPSettings.html[`HikariCPSettings`].
Its settings are enumerated by link:{doc-javadoc-url}/org/hibernate/cfg/HikariCPSettings.html[`HikariCPSettings`].

.Container-managed datasources
****
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
agroalDataSource.getConfiguration().connectionPoolConfiguration();
final AgroalConnectionFactoryConfiguration acfc = acpc.connectionFactoryConfiguration();
return new DatabaseConnectionInfoImpl(
AgroalConnectionProvider.class,
acfc.jdbcUrl(),
// Attempt to resolve the driver name from the dialect,
// in case it wasn't explicitly set and access to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public class C3P0ConnectionProvider
private static final String C3P0_STYLE_MAX_STATEMENTS = "c3p0.maxStatements";
private static final String C3P0_STYLE_ACQUIRE_INCREMENT = "c3p0.acquireIncrement";
private static final String C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD = "c3p0.idleConnectionTestPeriod";

//swaldman 2006-08-28: define c3p0-style configuration parameters for initialPoolSize, which
// hibernate sensibly lets default to minPoolSize, but we'll let users
// override it with the c3p0-style property if they want.
//swaldman 2006-08-28: define c3p0-style configuration parameters for initialPoolSize,
// which hibernate sensibly lets default to minPoolSize, but we'll
// let users override it with the c3p0-style property if they want.
private static final String C3P0_STYLE_INITIAL_POOL_SIZE = "c3p0.initialPoolSize";

private DataSource dataSource;
private Integer isolation;
private boolean autocommit;
Expand Down Expand Up @@ -144,6 +144,10 @@ public void configure(Map<String, Object> properties) {

loadDriverClass( jdbcDriverClass );

// c3p0 returns Connections with autocommit enabled, but for
// historical reasons we default to calling setAutocommit(false)
// 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 );

Expand All @@ -152,11 +156,12 @@ public void configure(Map<String, Object> properties) {
dataSource = createDataSource( jdbcUrl, connectionProps, poolSettings );

dbInfoProducer = dialect -> new DatabaseConnectionInfoImpl(
C3P0ConnectionProvider.class,
jdbcUrl,
jdbcDriverClass,
dialect.getVersion(),
Boolean.toString( autocommit ),
isolation != null ? ConnectionProviderInitiator.toIsolationNiceName( isolation ) : null,
isolation == null ? null : ConnectionProviderInitiator.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 ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.dialect.DatabaseVersion;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
import org.hibernate.internal.util.NullnessHelper;
import org.hibernate.internal.util.StringHelper;
Expand All @@ -27,6 +28,7 @@
public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
public static final String DEFAULT = "undefined/unknown";

private final Class<?> connectionProviderClass;
protected final String jdbcUrl;
protected final String jdbcDriver;
protected final DatabaseVersion dialectVersion;
Expand All @@ -36,13 +38,15 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
protected final Integer poolMaxSize;

public DatabaseConnectionInfoImpl(
Class<? extends ConnectionProvider> connectionProviderClass,
String jdbcUrl,
String jdbcDriver,
DatabaseVersion dialectVersion,
String autoCommitMode,
String isolationLevel,
Integer poolMinSize,
Integer poolMaxSize) {
this.connectionProviderClass = connectionProviderClass;
this.jdbcUrl = nullIfEmpty( jdbcUrl );
this.jdbcDriver = nullIfEmpty( jdbcDriver );
this.dialectVersion = dialectVersion;
Expand All @@ -54,6 +58,7 @@ public DatabaseConnectionInfoImpl(

public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect) {
this(
null,
determineUrl( settings ),
determineDriver( settings ),
dialect.getVersion(),
Expand All @@ -66,7 +71,7 @@ public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect)
}

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

@Override
Expand Down Expand Up @@ -111,6 +116,7 @@ public String toInfoString() {
"\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 );
}
Expand All @@ -127,6 +133,10 @@ private static String handleEmpty(Integer value) {
return value != null ? value.toString() : DEFAULT;
}

private static String handleEmpty(Class<?> value) {
return value != null ? value.getSimpleName() : DEFAULT;
}


@SuppressWarnings("deprecation")
private static String determineUrl(Map<String, Object> settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public boolean supportsAggressiveRelease() {
@Override
public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
return new DatabaseConnectionInfoImpl(
DatasourceConnectionProviderImpl.class,
null,
null,
dialect.getVersion(),
Expand Down
Loading
Loading