World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
title: "JDBC Catalog Connection Validation" slug: "/jdbc-catalog-connection-validation" keywords:
- jdbc
- catalog
- connection-pool license: "This software is licensed under the Apache License version 2."
This page describes connection validation for JDBC catalogs' datasource pools. The settings apply to the MySQL, PostgreSQL, Doris, StarRocks, ClickHouse, Hologres, and OceanBase catalogs.
Connection Validation
JDBC catalogs validate connections on borrow by default using the JDBC driver's
Connection.isValid() method. Gravitino does not force a validation SQL query, so the driver
can validate the connection after catalog or schema changes.
The default avoids a connection-reuse problem with MySQL URLs that have no default database.
DBCP caches SQL validation as a prepared statement. MySQL Connector/J retains the database used
when that statement was prepared and tries to switch back to it when executing the statement.
After an application calls setCatalog(), switching back to an empty original database can fail
even though the connection is still healthy. DBCP then discards that connection and opens another.
Connector/J's Connection.isValid() uses a ping instead of the cached statement, so validation
does not need to restore the statement's original database.
The benefit is preserving healthy pooled connections and avoiding repeated connection setup and
authentication caused by failed validation. Validation remains enabled and disconnected connections
are still replaced. This does not establish a general throughput improvement: other drivers may
implement isValid() using SQL, and validation can still require a network round trip.
For a driver that does not support Connection.isValid(), set the catalog property
gravitino.bypass.validationQuery to a SQL SELECT statement that returns at least one row.
Explicit validation queries are passed to DBCP without being overwritten. The
jdbc.pool.test-on-borrow property controls validation on borrow. See the
DBCP configuration reference
for other pool validation settings. DBCP-specific properties require the gravitino.bypass.
prefix in catalog configuration; the prefix is removed before passing them to DBCP.
For example, to use SQL validation with a driver that does not support Connection.isValid():
{
"jdbc.pool.test-on-borrow": "true",
"gravitino.bypass.validationQuery": "SELECT 1",
"gravitino.bypass.validationQueryTimeout": "5"
}
Choose a validation query supported by the driver and database. Disabling validation on borrow
alone does not make a driver without Connection.isValid() support compatible: DBCP also validates
a connection when initializing its connection factory.
For MySQL drivers affected by the catalog-switching problem, explicitly setting SELECT 1 restores
the cached-statement validation path and can reproduce connection churn. Leave the query unset when
the driver supports Connection.isValid().
Backend verification at create time
A relational JDBC catalog verifies that its backend is reachable when the catalog is created,
rather than deferring the first connection to a later operation. The verification opens a dedicated,
time-bounded one-off probe connection (separate from the pool used by normal operations), confirms
it, and closes it. If it fails, the create is rejected and rolled back (the catalog entity is not
persisted), and the caller receives a connection error that names the backend host:port and the
underlying driver message.
To register a catalog whose backend is temporarily unavailable, set the catalog property
verify-on-create to false, which skips the create-time check:
{
"verify-on-create": "false"
}
This setting skips only the backend connection probe. Driver/URL compatibility, boolean values, and pool bounds are still checked on catalog creation and alteration, without opening a database connection. Invalid alterations leave the stored configuration unchanged.
The default is true (verify on create). The connection is still opened lazily on first use when
the check is skipped.
Bounded timeouts
So that create-time verification fails fast instead of hanging, the timeout
jdbc.pool.connect-timeout-ms (default 30000 ms, maximum 2147483647) bounds a JDBC connection.
Gravitino translates this canonical millisecond value into each driver family's own parameters and
unit:
- MySQL Connector/J — also used by the Doris, StarRocks, and OceanBase catalogs:
connectTimeoutandsocketTimeoutin milliseconds. - PostgreSQL — also used by the Hologres catalog:
connectTimeout,socketTimeoutandloginTimeoutin seconds (rounded up, minimum 1, maximum2147483). The upper cap avoids overflow when the driver converts seconds back to integer milliseconds; near the millisecond upper bound, it shortens the configured timeout by less than one second. - ClickHouse:
connect_timeoutandsocket_timeoutin milliseconds (note the underscore names).
It is applied differently to the two kinds of connection:
- Pooled connections (normal operations) get only the connect timeout. A socket-read
timeout is deliberately not imposed there, because it would also abort legitimate long-running
operations on a healthy backend (a large
ALTER TABLE, a slow metadata query). - The create-time verification probe gets both the connect and the read (and, for PostgreSQL, login) timeout. The probe runs under the metalake write lock, so it must be fully bounded: a backend that accepts the TCP connection but never speaks the database protocol (a load balancer with a dead backend, a wrong port, a proxy that accepts and never forwards) would otherwise block a connect-only bound forever and stall the whole metalake.
A timeout value you set directly on the connection or in the jdbc-url query string is never
overridden; drivers whose family is not recognized are left unbounded by Gravitino. The pooled
timeout is applied as a per-connection bound, so the pool stays lazy and no connection is opened
until the first real round trip.
Driver adapter for physical connection deadlines
Connector implementations can opt into org.apache.gravitino.utils.BoundedConnectionDriver
when their JDBC driver does not enforce a login timeout. Wrap the driver with a positive
timeout in milliseconds and pass the wrapper to the connection pool as its driver. No
existing connector is automatically switched to this adapter by this addition.
Only Driver.connect runs on a worker. Metadata, validation and SQL remain on the caller;
this does not introduce delayed DDL or an automatic retry. A connection returned after
timeout/interruption is closed by the worker and never handed to the caller or pool.
While an abandoned attempt is connecting, waiting for an in-flight interrupt callback, or closing,
new attempts through that wrapper fail immediately. Pool waits, connection validation, SQL and total request duration retain
their own budgets.
The core-loaded adapter shares at most 64 daemon connection workers and 64 daemon cancellation
threads per server JVM, with no queue in either pool. A single additional daemon thread dispatches
bounded cancellation retries without running driver code. Saturation fails new connects with SQLTransientConnectionException, including otherwise
healthy connects. An abandoned attempt's worker is interrupted from a separate cancellation thread,
not the cancelling caller, because a driver's interrupt callback can block synchronously; the
interrupt is delivered only while the worker is still connecting. It releases the worker only from
interruptible waits: blocking java.net socket I/O is not interruptible on JDK 17, so a driver
blocked in a socket connect or read keeps its worker until its own socket timeout, and a driver or
close that never returns keeps it until the server restarts.
Abandoned and completed-after-abandonment attempts are logged at INFO; worker-admission rejections
are aggregated into at most one WARN log line a minute. A rejected cancellation can be submitted up to three more times, after delays of 10, 20 and 40 ms,
with at most 64 attempts pending retry across the adapter. Only cancellation admission is retried;
Driver.connect and SQL are never replayed. The retry dispatcher never interrupts a worker itself.
Retries stop when the driver returns. Retry exhaustion, full retry capacity or a rejected retry
schedule is logged at WARN. Its worker remains quarantined until the driver and cleanup return;
if they never return, recovery requires a server restart. Cancellation is best-effort, not
forced thread termination. Completed workers
restore their context class loader. The adapter does not bound arbitrary driver constructor
work or clean up driver-owned global threads.
Each attempt, its driver interrupt callback, and its late connection cleanup use the submitting caller's access-control context, JAAS subject, and context class loader. Cancellation threads restore their prior context after the callback. Interrupts consumed while waiting for cancellation are restored after connection cleanup. Shared workers are created without inheriting the first caller's subject, so reusing a worker does not reuse that caller's JAAS subject.
This guarantee covers access-control contexts and JAAS subjects, not arbitrary ThreadLocal
or InheritableThreadLocal state. Drivers using the shared workers must clean up their own
thread-local state and must not depend on inherited thread locals for caller authentication.
Drivers that cannot meet these requirements must not opt into this adapter. Isolating workers
by catalog alone would not isolate different users of the same catalog.
A JDBC catalog whose driver does not enforce a login timeout can also bound its runtime physical
connections with this adapter by overriding JdbcCatalogOperations#configureDataSource and
installing it as the data source's driver. The hook must not borrow connections, start the pool,
retain the data source, or change its validated URL, credentials and pool settings. Pool waits, SQL
execution and tree-lock acquisition are separate from this physical connection deadline.