Skip to content

Commit cff532d

Browse files
fix(bigquery-jdbc): enforce JDBC connection pooling lifecycle, state reset, and physical isolation (#15202)
### Summary Consolidates the 3-PR JDBC connection pooling stack (#15202, #15203, #15204) onto a single branch targeting `main` so all CI checks run against the complete change and the stack lands atomically. Fixes physical connection isolation, JDBC 4.3 `PooledConnection` / logical `Connection` lifecycle contracts, and mutable connection/transaction/session state reset on pool return: - **Connection state reset hook (`BigQueryConnection.resetForReuse()`):** Closes remaining open `Statement` instances tracked in `openStatements`, resets `autoCommit` to `true`, rolls back any active transaction without starting a follow-up `BEGIN TRANSACTION;` (`rollbackImpl(false)` in both `resetForReuse()` and `closeImpl()`), aborts driver-created BigQuery sessions (`CALL BQ.ABORT_SESSION();` when `EnableSession=1`) even when the calling thread is interrupted while preserving caller-configured `QueryProperties=session_id=...` properties, surfaces `BigQueryException` on pooled session abort while preserving un-aborted transaction/session state for fallback physical `close()`, clears `clientInfo` and `sqlWarnings` (backed by `CopyOnWriteArrayList` for concurrent snapshot reads), and restores `holdability`. - **Lifecycle serialization, rejection-safe `abort(Executor)` & MDC preservation:** Serializes `resetForReuse()` and `closeImpl()` via `lifecycleLock`; coordinates `abort(Executor)` and `close()` via `closeStateLock` so `isClosed` remains `false` until `executor.execute` accepts/starts while gating `checkClosed()` on `closeInitiated`; honors configured `Timeout` and inspects `JobStatus` on transaction commit/rollback job waits; and saves/restores outer thread MDC `connectionId` in `BigQueryJdbcMdc.registerInstance`, `close()`, and `abort(Executor)`. - **JDBC 4.3 `BigQueryPooledConnection` & dynamic handle/child proxies:** Replaces `BigQueryPooledConnectionWrapper` with `PooledConnectionInvocationHandler` backed by a per-handle `ReentrantReadWriteLock` shared with pooled `Statement` and `DatabaseMetaData` proxies so operational calls hold the read lock during RPCs while `close()`, handle reclaim, and physical teardown await quiescence. Wraps child `Statement` / `PreparedStatement` / `CallableStatement` and `DatabaseMetaData` objects (and attaches pooled proxies on `BigQueryStatement` for `ResultSet.getStatement()`) so `getConnection()` enforces `parentHandler.checkClosed()` and returns the logical `Connection` handle. Fires `connectionErrorOccurred` and physically closes `BigQueryConnection` on reset failures or out-of-band physical closes, dispatches `abort(Executor)` teardown asynchronously, isolates listener `RuntimeException`s, and scopes MDC inside `physicalLock`. - **Dedicated physical connections & concurrent LIFO checkout (`PooledConnectionDataSource` / `PooledConnectionListener`):** Removes the cached shared `bqConnection` field from `PooledConnectionDataSource` so cache misses open a dedicated `BigQueryConnection`, recycles idle physical connections into fresh `BigQueryPooledConnection` holders while retiring prior holders (`State.RETIRED`), separates the internal pool listener from public `ListenerPoolSize` slots with sticky external ownership on `addConnectionEventListener`, uses atomic LIFO (`addFirst`/`pollFirst`) checkout under `poolLock`, closes excess driver-owned idle connections at `ConnectionPoolSize` (`0` = unbounded), and clarifies `ConnectionPoolSize` and `ListenerPoolSize` in `docs/USER_GUIDE.md`. ### Behavior Changes - Every cache miss on `PooledConnectionDataSource.getPooledConnection()` opens a dedicated physical `BigQueryConnection`, and idle-pool hits wrap the recycled physical connection in a new `PooledConnection` while retiring the previous holder. - `Connection.close()` on a pooled handle synchronously runs state/transaction/session reset RPCs when needed and propagates any reset `SQLException` after firing `connectionErrorOccurred`. - `BigQueryPooledConnection.close()` physically closes `BigQueryConnection` and removes the holder from `PooledConnectionListener` without firing `connectionClosed`. - `BigQueryPooledConnection.getConnection()` while a handle is open invalidates the prior handle and resets connection state without firing `connectionClosed` (`PooledConnection#getConnection()`, JDBC 4.3 section 11.1). - Returning a connection when the built-in idle pool is at `ConnectionPoolSize` closes the excess physical connection inline on the caller thread (`ConnectionPoolSize=0` retains all idle connections). - Operational calls on a closed logical handle or retained child `Statement` / `DatabaseMetaData` throw `BigQueryJdbcException` (`SQLClientInfoException` with `ClientInfoStatus.REASON_UNKNOWN` for `setClientInfo`). ### Testing - Added/updated unit tests in `BigQueryConnectionTest`, `BigQueryJdbcMdcTest`, `BigQueryPooledConnectionTest`, `PooledConnectionDataSourceTest`, and `PooledConnectionListenerTest` (`1,384` unit tests passing). - Added integration tests in `ITConnectionPoolingTest` verifying independent concurrent checkouts and end-to-end session and uncommitted-transaction reset on pool return.
1 parent d61b6d3 commit cff532d

14 files changed

Lines changed: 4157 additions & 760 deletions

‎java-bigquery-jdbc/docs/USER_GUIDE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ String url = "jdbc:bigquery://https://bigquery.googleapis.com:443"
272272
| `RetryInitialDelay` | `0` | Initial delay (in seconds) before executing the first retry attempt. |
273273
| `RetryMaxDelay` | `0` | Maximum delay limit (in seconds) between retry attempts. |
274274
| `MetaDataFetchThreadCount` | `32` | Thread pool size used to parallelize `DatabaseMetaData` catalog RPC calls. |
275-
| `ConnectionPoolSize` | `10` | Maximum size of the internal connection pool when connection pooling is enabled. |
276-
| `ListenerPoolSize` | `10` | Maximum size of the listener thread pool when connection pooling is enabled. |
275+
| `ConnectionPoolSize` | `10` | Maximum number of idle physical connections retained in `PooledConnectionDataSource`'s built-in pool (`0` = unbounded). Latched from the first physical connection checkout; changing it on the same `PooledConnectionDataSource` afterward has no effect. |
276+
| `ListenerPoolSize` | `10` | Maximum number of `ConnectionEventListener` instances that can be registered on a single `PooledConnection` (`0` = unbounded). |
277277
| `RequestReason` | `null` | Reason string passed in the `x-goog-request-reason` HTTP header for auditing. |
278278
279279
### OpenTelemetry & Cloud Observability Exporters

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ public int getConcurrency() throws SQLException {
290290
@Override
291291
public Statement getStatement() throws SQLException {
292292
checkClosed();
293-
return this.statement;
293+
if (this.statement == null) {
294+
return null;
295+
}
296+
Statement proxy = this.statement.getLogicalStatementProxy();
297+
return proxy != null ? proxy : this.statement;
294298
}
295299

296300
@Override

0 commit comments

Comments
 (0)