Este contenido no está disponible en el idioma seleccionado.
Chapter 7. Using Agroal database connection pool
Agroal is a fast and lightweight database connection pool. The agroal-spring-boot-starter is a Red Hat build of Camel Spring Boot starter project that simplifies the integration of Agroal into Spring Boot applications. The io.agroal.springframework.boot.AgroalDataSource class is a Spring Boot-compatible implementation of the javax.sql.DataSource interface, providing a high-performance connection pool for database operations. It is a part of the Agroal connection pooling library developed by Red Hat.
7.1. Key Features Copiar enlaceEnlace copiado en el portapapeles!
The key features of Agroal are as follows:
- High-Performance Connection Pooling: Efficiently manages database connections with configurable pool settings
- Spring Boot Integration: Seamlessly integrates with Spring Boot’s auto-configuration
- Transaction Support: Provides JTA transaction integration through Narayana
- Connection Monitoring: Includes detailed logging and metrics for connection lifecycle
- Leak Detection: Built-in connection leak detection and reporting
7.2. Dependencies Copiar enlaceEnlace copiado en el portapapeles!
When using Agroal with Red Hat build of Camel Spring Boot, add the necessary dependencies to your pom.xml.
-
camel-spring-boot-starter: For seamless integration of Camel with Spring Boot. -
camel-jdbc-starter: If you are using the Camel JDBC component. -
agroal-spring-boot-starter: For Agroal integration with Spring Boot. - Your database driver (e.g., postgresql, mysql).
Dependencies
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jdbc-starter</artifactId>
</dependency>
<dependency>
<groupId>io.agroal</groupId>
<artifactId>agroal-spring-boot-starter</artifactId>
</dependency>
<!-- Replace with your database driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
7.3. Configuring Agroal Copiar enlaceEnlace copiado en el portapapeles!
Following section explains how to configure and use Agroal in your application.
Procedure
- Add the necessary dependencies as shown above.
Define database connection details and Agroal-specific properties in the
application.propertiesor theapplication.ymlfile. Agroal will use these to create and manage the connection pool.Connection Pool Settings
- Maximum Pool Size: Configure the maximum number of connections in the pool
- Minimum Pool Size: Set the minimum number of connections maintained
- Initial Pool Size: Specify the initial number of connections created
Database Connection Properties
- JDBC URL: Database connection URL
- Username: Database username
- Password: Database password
- Driver Class: JDBC driver class name
- Sample database connection properties
# Database connection spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=myuser spring.datasource.password=mypassword # Agroal-specific settings (in seconds) spring.datasource.agroal.max-size=20 spring.datasource.agroal.min-size=5 spring.datasource.agroal.initial-size=5 spring.datasource.agroal.acquisition-timeout=30Define Camel Routes.
Create the Camel routes that interact with your database using the JDBC component. Camel’s JDBC component can be configured to use the DataSource provided by Spring Boot, which is managed by Agroal.
import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class MyDatabaseRoute extends RouteBuilder { @Override public void configure() throws Exception { from("timer:myTimer?period=5000") .setBody().constant("SELECT * FROM my_table") .to("jdbc:dataSource") // 'dataSource' is the default name for Spring's DataSource .log("Query Result: ${body}"); } }Ensure your main Spring Boot application class is correctly set up to run the application.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AgroalCamelApplication { public static void main(String[] args) { SpringApplication.run(AgroalCamelApplication.class, args); } }
Spring Boot’s auto-configuration detects the agroal-spring-boot-starter and configures Agroal as the connection pool for your DataSource bean. The camel-spring-boot-starter automatically configures a CamelContext and makes it available as a Spring bean. When using the jdbc component in your Camel routes, it automatically leverages the DataSource bean managed by Agroal, ensuring efficient and robust database connectivity.
7.4. API changes in AgroalDataoSourceAutoConfiguration Copiar enlaceEnlace copiado en el portapapeles!
When upgrading from version 4.10.3 to 4.10.7 of Red Hat build of Camel Spring Boot, the API changes in AgroalDataSourceAutoConfiguration require code updates, which specifically affect programmatic DataSource configurations. Projects that use programmatic DataSource definitions (rather than auto-configuration) will experience compilation errors and need code modifications. After upgrading from version 4.10.3 to 4.10.7, the AgroalDataSourceAutoConfiguration constructor requires additional parameters wrapped in ObjectProvider.
Following sample shows the required changes.
Before version 4.10.7
@ConfigurationProperties("app.datasource.ds1.agroal")
public AgroalDataSource firstDataSource(
@Qualifier("ds1properties") DataSourceProperties properties,
JtaTransactionManager jtaPlatform,
XAResourceRecoveryRegistry xaResourceRecoveryRegistry,
ObjectProvider<AgroalDataSourceJndiBinder> jndiBinder) {
return new AgroalDataSourceAutoConfiguration(jtaPlatform, xaResourceRecoveryRegistry)
.dataSource(properties, false, false, jndiBinder);
}
After upgrading to version 4.10.7
@ConfigurationProperties("app.datasource.ds1.agroal")
public AgroalDataSource firstDataSource(
@Qualifier("ds1properties") DataSourceProperties properties,
ObjectProvider<JtaTransactionManager> jtaPlatform,
ObjectProvider<XAResourceRecoveryRegistry> xaResourceRecoveryRegistry,
ObjectProvider<AgroalDataSourceJndiBinder> jndiBinder,
ObjectProvider<AgroalSecurityProvider> securityProvider) {
return new AgroalDataSourceAutoConfiguration(jtaPlatform, xaResourceRecoveryRegistry, jndiBinder, securityProvider)
.dataSource(properties, true, false, false, new ArrayList<Object>(), new ArrayList<Object>());
}
7.5. Monitoring and Logging Copiar enlaceEnlace copiado en el portapapeles!
The class includes built-in logging capabilities through an internal LoggingListener that tracks:
- Connection creation and destruction
- Connection acquisition and return
- Connection validation events
- Connection leak detection
The metrics will be enabled using the follwing property.
spring.datasource.agroal.metrics = true
This property exposes the following metrics documented in the javadoc
- agroal.acquire.count
- agroal.awaiting.count
- agroal.blocking.time.average
- agroal.blocking.time.max
- agroal.blocking.time.total
- agroal.connections.active.count
- agroal.connections.available.count
- agroal.connections.creation.count
- agroal.connections.creation.time.average
- agroal.connections.creation.time.max
- agroal.connections.creation.time.total
- agroal.connections.destroy.count
- agroal.connections.flush.count
- agroal.connections.invalid.count
- agroal.connections.max.used.count
- agroal.connections.reap.count
- agroal.leak.detection.count
7.6. Basic DataSource Configuration Copiar enlaceEnlace copiado en el portapapeles!
Following section explains the basic datasource configuration using Agroal.
Properties
# DataSource name and implementation spring.datasource.agroal.name=<datasource-name> spring.datasource.agroal.implementation=<AGROAL|AGROAL_POOLLESS>Database connection settings
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.datasource.driver-class-name=org.postgresql.DriverConnection Pool Configuration
# Pool sizing spring.datasource.agroal.max-size=20 spring.datasource.agroal.min-size=5 spring.datasource.agroal.initial-size=5 # Validation settings spring.datasource.agroal.validate-on-borrow=true spring.datasource.agroal.connection-validator-name=<validator-name> spring.datasource.agroal.exception-sorter-name=<sorter-name>Timeout Configuration
# Connection timeouts (in seconds) spring.datasource.agroal.acquisition-timeout=30 spring.datasource.agroal.foreground-validation-timeout=5 spring.datasource.agroal.idle-timeout=300 spring.datasource.agroal.leak-timeout=0 spring.datasource.agroal.lifetime-timeout=0 spring.datasource.agroal.validation-timeout=5Connection Factory Configuration
spring.datasource.agroal.initial-sql=SELECT 1 spring.datasource.agroal.auto-commit=true spring.datasource.agroal.track-resources=true spring.datasource.agroal.jdbc-transaction-isolation=2Recovery credentials (for XA transactions)
spring.datasource.agroal.recovery-username=recovery_user spring.datasource.agroal.recovery-password=recovery_passwordAdvanced Configuration
- Monitoring and metrics
spring.datasource.agroal.metrics=trueAdvanced pool settings
spring.datasource.agroal.enhanced-leak-report=false spring.datasource.agroal.flush-on-close=falseJDBC and XA properties (as nested properties)
spring.datasource.agroal.jdbc-properties.key1=value1 spring.datasource.agroal.jdbc-properties.key2=value2 spring.datasource.agroal.xa-properties.key1=value1 spring.datasource.agroal.xa-properties.key2=value2Transaction Integration
# JTA Transaction Management # Note: These are typically configured programmatically through beans # spring.datasource.agroal.jta-transaction-integration=<bean-reference> # spring.datasource.agroal.jta-transaction-manager=<bean-reference>
7.7. Property Reference Table Copiar enlaceEnlace copiado en el portapapeles!
| Property | Type | Default | Description |
|---|---|---|---|
| spring.datasource.agroal.name | String | <default> | DataSource name for logging |
| spring.datasource.agroal.implementation | String | AGROAL | DataSource implementation (AGROAL, AGROAL_POOLLESS) |
| spring.datasource.agroal.max-size | Integer | 10 | Maximum number of connections in pool |
| spring.datasource.agroal.min-size | Integer | 0 | Minimum number of connections in pool |
| spring.datasource.agroal.initial-size | Integer | 0 | Initial number of connections created |
| spring.datasource.agroal.validate-on-borrow | Boolean | false | Validate connections when borrowed |
| spring.datasource.agroal.connection-validator-name | String | - | Connection validator implementation |
| spring.datasource.agroal.exception-sorter-name | String | - | Exception sorter implementation |
| spring.datasource.agroal.acquisition-timeout | Integer | - | Connection acquisition timeout (seconds) |
| spring.datasource.agroal.foreground-validation-timeout | Integer | - | Foreground validation timeout (seconds) |
| spring.datasource.agroal.idle-timeout | Integer | - | Connection idle timeout (seconds) |
| spring.datasource.agroal.leak-timeout | Integer | - | Connection leak detection timeout (seconds) |
| spring.datasource.agroal.lifetime-timeout | Integer | - | Connection maximum lifetime (seconds) |
| spring.datasource.agroal.validation-timeout | Integer | - | Connection validation timeout (seconds) |
| spring.datasource.agroal.initial-sql | String | - | SQL to execute on new connections |
| spring.datasource.agroal.auto-commit | Boolean | true | Enable auto-commit on connections |
| spring.datasource.agroal.track-resources | Boolean | false | Track JDBC resources |
| spring.datasource.agroal.recovery-username | String | - | XA recovery username |
| spring.datasource.agroal.recovery-password | String | - | XA recovery password |
| spring.datasource.agroal.jdbc-transaction-isolation | Integer | - | JDBC transaction isolation level |
| spring.datasource.agroal.metrics | Boolean | false | Enable metrics collection |
| spring.datasource.agroal.enhanced-leak-report | Boolean | false | Enhanced leak reporting |
| spring.datasource.agroal.flush-on-close | Boolean | false | Flush connections on datasource close |
| spring.datasource.agroal.jdbc-properties.* | Map | - | Additional JDBC properties |
| spring.datasource.agroal.xa-properties.* | Map | - | XA datasource properties |