Class Jdbc
- All Implemented Interfaces:
Middleware,AutoCloseable
var jdbc = Jdbc.builder(app, applicationOwnedDataSource)
.maxConnections(16)
.routeLimit(ordersRoute, 4)
.readiness(Health.builder(app))
.build()
.register();
try (var connection = jdbc.dataSource().getConnection()) {
// ordinary JDBC; the gate has already bounded this request
}
Registered as middleware with register() or middleware(), the gate acquires
one permit before the rest of the chain runs and releases it once the response is available. A
mapped error response passes through afterError(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.http.Response), which also releases a permit; both
paths are idempotent, so no permit is held after the request and none is released twice. When no
permit is available within the configured bounded wait, the handler is not run: the answer is a
503 problem response with code db_capacity, counted on axiom.admission.rejected
with the fixed reason tag db_capacity. An aggregate cap and optional per-route caps are
enforced together; a per-route cap is never larger than the aggregate cap.
Ownership. The DataSource passed to builder(com.jsgalactic.axiom.application.Application, javax.sql.DataSource) belongs to the
application. The gate and its wrapper never close it, never change its settings and never close a
connection the application checked out except through that connection's own close().
Building a gate registers a drain action that stops new permits and a close action that closes
still-open wrapped connections only when Jdbc.Builder.closeConnectionsOnShutdown() was called.
Without that opt-in, application close touches no connection and no source.
The wrapper returned by dataSource() counts checkouts, releases and contention on the
existing Metrics SPI with one bounded name tag. The gate's own series carry a
method and route template from a bounded set. Health probes use the unwrapped source, so they do
not consume permits or appear in the connection counters. Instances are immutable and thread-safe
after construction; the source must be thread-safe too.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCollects bounds and hooks beforeJdbc.Builder.build().Nested classes/interfaces inherited from interface com.jsgalactic.axiom.context.Middleware
Middleware.Next -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of the readiness check added byJdbc.Builder.readiness(Health.Builder).static final StringProblem code and bounded rejection reason used when a permit is unavailable. -
Method Summary
Modifier and TypeMethodDescriptionafterError(Context context, Response response) Releases a permit for a response produced from an exception.static Jdbc.Builderbuilder(Application application, DataSource source) Starts a builder for one application-owned source.voidclose()Stops new permits and closes the connections this gate tracks, if the application opted into owning them.Returns the counted wrapper applications use for ordinary JDBC work.handle(Context context, Middleware.Next next) Acquires a permit before the rest of the chain and releases it once a response is available.Returns a readiness check that runs the bounded query fromJdbcOptions.probeQuery()on the unwrapped source.Returns the middleware that acquires and releases permits.options()Returns the validated bounds of this gate.register()Registers this gate as global application middleware.
-
Field Details
-
REJECTION_CODE
Problem code and bounded rejection reason used when a permit is unavailable.- See Also:
-
HEALTH_NAME
Name of the readiness check added byJdbc.Builder.readiness(Health.Builder).- See Also:
-
-
Method Details
-
builder
Starts a builder for one application-owned source. The application supplies its own driver, connection settings and lifecycle; nothing is opened or configured here.- Parameters:
application- application whose lifecycle owns the gatesource- application-owned source, never closed by the gate- Returns:
- a new builder
- Throws:
NullPointerException- if an argument is null
-
options
Returns the validated bounds of this gate.- Returns:
- immutable options
-
dataSource
Returns the counted wrapper applications use for ordinary JDBC work. The delegate source is not closed when this wrapper or the application closes.- Returns:
- thread-safe source that counts checkouts and releases
-
middleware
Returns the middleware that acquires and releases permits. It is this gate itself, so the instance can be registered globally or on one group.- Returns:
- thread-safe middleware
-
register
Registers this gate as global application middleware. Routes registered later are covered as well. An alternative is to passmiddleware()to one group or route.- Returns:
- this gate
- Throws:
IllegalStateException- if the application is closed
-
healthCheck
Returns a readiness check that runs the bounded query fromJdbcOptions.probeQuery()on the unwrapped source. It is UP only when the statement returns at least one row. A driver failure, an invalid statement, an interrupted probe or a timeout reports DOWN without the SQL, the URL, credentials or the exception message leaving the check.- Returns:
- check suitable for any named Axiom readiness hook
-
handle
Acquires a permit before the rest of the chain and releases it once a response is available. The permit is held while the handler runs, including any database work it does throughdataSource().- Specified by:
handlein interfaceMiddleware- Parameters:
context- request context, used only for its matched route identitynext- rest of the chain- Returns:
- the handler's response
- Throws:
Exception- if a permit cannot be acquired or the rest of the chain fails
-
afterError
Releases a permit for a response produced from an exception.handle(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.context.Middleware.Next)already released it on every path, including failures that bypass error mapping, so this call is a no-op for those requests and leaves the response unchanged.- Specified by:
afterErrorin interfaceMiddleware- Parameters:
context- failed request's contextresponse- mapped error response- Returns:
- the unchanged response
-
close
public void close()Stops new permits and closes the connections this gate tracks, if the application opted into owning them. The application-providedDataSourceis never closed. Idempotent.- Specified by:
closein interfaceAutoCloseable
-