Repository navigation
Commit cff532d
authored
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
File tree
- java-bigquery-jdbc
- docs
- src
- main/java/com/google/cloud/bigquery/jdbc
- test/java/com/google/cloud/bigquery/jdbc
- it
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
276 | | - | |
| 275 | + | |
| 276 | + | |
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
293 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
294 | 298 | | |
295 | 299 | | |
296 | 300 | | |
| |||
0 commit comments