Repository navigation
CorrelationIdConnectionProvider
A Hibernate ConnectionProvider decorator that injects the current Micrometer B3 trace ID into the PostgreSQL transaction via the app.correlation_id GUC. Part of the Correlation ID Audit Propagation pattern.
public CorrelationIdConnectionProvider(ConnectionProvider delegate, Tracer tracer)| Parameter | Type | Description |
|---|---|---|
delegate |
ConnectionProvider |
The real pooled provider (e.g. DatasourceConnectionProviderImpl) |
tracer |
Tracer |
Micrometer tracer providing the current B3 span |
Returns a pooled JDBC connection. When a Micrometer span is active with a non-blank trace ID, wraps the connection in DeferredCorrelationIdConnection, which binds the trace ID lazily on first statement use. Without an active span, a noop span, or a usable trace ID, returns the raw delegate connection unchanged.
Forwards configuration to the delegate if it implements Configurable.
Forwards lifecycle stop to the delegate if it implements Stoppable.
Delegates directly to the underlying provider.
Delegates to the underlying provider.
Delegates to the underlying provider.
Delegates to the underlying provider.
A private final inner class implementing java.sql.Connection. Wraps the real connection and executes SELECT set_config('app.correlation_id', ?, true) exactly once, immediately before the first createStatement, prepareStatement, prepareCall, or setSavepoint call.
Every other Connection method is forwarded verbatim to the target.
SELECT set_config('app.correlation_id', ?, true)The third argument true marks the setting as transaction-local (is_local), so PostgreSQL discards it on commit/rollback. No explicit reset is required.
-
io.micrometer.tracing.Tracer— readscurrentSpan()for trace context -
io.micrometer.tracing.Span— readscontext().traceId() -
org.hibernate.engine.jdbc.connections.spi.ConnectionProvider— decorator target -
org.hibernate.service.spi.Configurable/Stoppable— lifecycle forwarding
-
GlobalConfiguration.correlationIdConnectionProviderCustomizer— creates and registers this provider as aHibernatePropertiesCustomizerbean -
V1.0.6__audit_correlation_id.sql— the trigger readscurrent_setting('app.correlation_id', true)
- GraalVM-safe: No reflection, no proxies, no runtime bytecode. All dependencies are constructor-injected.
- Deferred binding is required: Eager binding breaks SERIALIZABLE isolation. See Correlation-ID Audit Trigger — Deferred Binding and Test Hygiene for details.
-
Pool-wide
auto-commit=falserequired:set_config(..., true)needs an active transaction block. - Native-image not validated: GraalVM native-image compilation and runtime validation remain a follow-up task.
-
@SuppressWarnings({"deprecation", "removal"})on customizer: TheDatasourceConnectionProviderImplconstructor andconfigure()method use deprecated Hibernate APIs; these are intentional and must not be "simplified."
Registered automatically via GlobalConfiguration:
@Bean
@SuppressWarnings({"deprecation", "removal"})
public HibernatePropertiesCustomizer correlationIdConnectionProviderCustomizer(
DataSource dataSource, Tracer tracer) {
return hibernateProperties -> {
DatasourceConnectionProviderImpl delegate = new DatasourceConnectionProviderImpl();
delegate.setDataSource(dataSource);
CorrelationIdConnectionProvider provider = new CorrelationIdConnectionProvider(delegate, tracer);
provider.configure(hibernateProperties);
hibernateProperties.put(JdbcSettings.CONNECTION_PROVIDER, provider);
};
}-
No active span: Returns raw connection,
current_setting('app.correlation_id', true)staysNULLin the trigger. -
Noop span: Same as no active span — provider checks
span.isNoop(). - Blank trace ID: Returns raw connection without wrapping.
- Connection reuse (Hikari pool): Transaction-local GUC ensures no bleed between sequential transactions on the same physical connection.
- Correlation ID Audit Propagation — the architecture this component implements
- Correlation-ID Audit Trigger — Deferred Binding and Test Hygiene — pattern and test hygiene rules
- Spring Boot Tracing in Integration Tests — the no-op tracer gotcha
CorrelationIdConnectionProvider.javaGlobalConfiguration.javaV1.0.6__audit_correlation_id.sqlAbstractTestContainerIntegrationTest.javaCorrelationIdAuditIntegrationTest.javaDistrictVolumeCorrelationIdAuditIntegrationTest.java
Merged via PR #1258.
This wiki documents NR Waste Plus for users and developers. Verify security and deployment guidance against the current application configuration before applying it elsewhere.
- Report an issue or ask a question in the GitHub repository.
- Follow the Contributing Guidelines to propose an update.
- Read the Code of Conduct.
Collection indexes support human browsing and machine retrieval. Keep both current when adding or moving wiki pages.
NR Waste Plus Wiki | GitHub Repository