Class Jdbc

java.lang.Object
com.jsgalactic.axiom.jdbc.Jdbc
All Implemented Interfaces:
Middleware, AutoCloseable

public final class Jdbc extends Object implements Middleware, AutoCloseable
Bounds the database concurrency of an Axiom application around a source the application owns and configures.

 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.

  • Field Details

  • Method Details

    • builder

      public static Jdbc.Builder builder(Application application, DataSource source)
      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 gate
      source - application-owned source, never closed by the gate
      Returns:
      a new builder
      Throws:
      NullPointerException - if an argument is null
    • options

      public JdbcOptions options()
      Returns the validated bounds of this gate.
      Returns:
      immutable options
    • dataSource

      public DataSource 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

      public Middleware 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

      public Jdbc register()
      Registers this gate as global application middleware. Routes registered later are covered as well. An alternative is to pass middleware() to one group or route.
      Returns:
      this gate
      Throws:
      IllegalStateException - if the application is closed
    • healthCheck

      public HealthCheck healthCheck()
      Returns a readiness check that runs the bounded query from JdbcOptions.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

      public Response handle(Context context, Middleware.Next next) throws Exception
      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 through dataSource().
      Specified by:
      handle in interface Middleware
      Parameters:
      context - request context, used only for its matched route identity
      next - 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

      public Response afterError(Context context, Response response)
      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:
      afterError in interface Middleware
      Parameters:
      context - failed request's context
      response - 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-provided DataSource is never closed. Idempotent.
      Specified by:
      close in interface AutoCloseable