Repository navigation
fix(bigquery-jdbc): enforce JDBC connection pooling lifecycle, state reset, and physical isolation - #15202
Open
keshavdandeva wants to merge 3 commits into
Open
fix(bigquery-jdbc): enforce JDBC connection pooling lifecycle, state reset, and physical isolation#15202keshavdandeva wants to merge 3 commits into
keshavdandeva wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a resetForReuse() method to BigQueryConnection to safely reset mutable JDBC connection state, close open statements, roll back active transactions without starting a follow-up transaction, and abort driver-created sessions before the connection is returned to a pool. It also refactors exception handling and adds comprehensive unit tests to verify these behaviors. The reviewer suggested resetting the mutable isReadOnlyTokenUsed state to false during connection reuse to prevent state leakage across connections.
keshavdandeva
force-pushed
the
fix/jdbc-pooling-1-connection-reset
branch
from
November 24, 2026 22:46
4181266 to
151cf1d
Compare
Neenu1995
reviewed
Nov 26, 2026
keshavdandeva
force-pushed
the
fix/jdbc-pooling-1-connection-reset
branch
from
November 27, 2026 14:21
151cf1d to
41da6f5
Compare
Neenu1995
approved these changes
Nov 27, 2026
…upport - Add package-private BigQueryConnection.resetForReuse() to close open statements, reset autoCommit to true, roll back active transactions without starting a follow-up transaction, abort driver-created sessions even when the calling thread is interrupted, surface BigQueryException on pooled session abort while preserving un-aborted session/transaction state for physical teardown, clear clientInfo and SQLWarnings, and restore holdability. - Serialize resetForReuse() and closeImpl() via lifecycleLock, pass startNextTransaction=false from closeImpl() so physical close does not start a new transaction on caller-managed sessions, and preserve outer caller MDC context in BigQueryJdbcMdc.registerInstance, close(), and abort(Executor). - Coordinate BigQueryConnection.abort(Executor) and close() via closeStateLock so rejecting executors never expose transient isClosed=true or cause concurrent close() to return early, gate checkClosed() on closeInitiated, and honor configured Timeout on transaction commit/rollback job waits. - Track optional pooled logical Connection and Statement proxies on BigQueryStatement so ResultSet.getStatement().getConnection() returns the logical handle.
…dle guards - Replace BigQueryPooledConnectionWrapper boilerplate with PooledConnectionInvocationHandler backed by a per-handle ReentrantReadWriteLock so close/reclaim/teardown wait for in-flight RPCs before resetting or closing. - Wrap pooled Statement and DatabaseMetaData instances so getConnection() enforces parentHandler.checkClosed() and returns the logical handle, and retained DatabaseMetaData instances reject operations after handle close. - Fire connectionErrorOccurred when an active or idle physical connection was closed out-of-band, isolate listener RuntimeExceptions, execute abort(Executor) teardown asynchronously, populate SQLClientInfoException failedProperties, and use method-scoped MDC preserving outer caller context.
…eckout in PooledConnectionDataSource - Open a dedicated physical BigQueryConnection on cache misses and recycle idle physical connections into fresh BigQueryPooledConnection holders while retiring prior holders. - Separate PooledConnectionDataSource's internal pool listener from public ConnectionEventListener slots, make external listener detachment sticky across removeConnectionEventListener -> close(), and preserve overflow teardown for standalone PooledConnectionListener instances. - Use LIFO (addFirst/pollFirst) idle reuse in PooledConnectionListener, close excess driver-owned idle connections at capacity, and clarify ConnectionPoolSize and ListenerPoolSize in docs/USER_GUIDE.md.
keshavdandeva
force-pushed
the
fix/jdbc-pooling-1-connection-reset
branch
from
November 27, 2026 17:06
41da6f5 to
2064902
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Consolidates the 3-PR JDBC connection pooling stack (#15202, #15203, #15204) onto a single branch targeting
mainso all CI checks run against the complete change and the stack lands atomically.Fixes physical connection isolation, JDBC 4.3
PooledConnection/ logicalConnectionlifecycle contracts, and mutable connection/transaction/session state reset on pool return:BigQueryConnection.resetForReuse()): Closes remaining openStatementinstances tracked inopenStatements, resetsautoCommittotrue, rolls back any active transaction without starting a follow-upBEGIN TRANSACTION;(rollbackImpl(false)in bothresetForReuse()andcloseImpl()), aborts driver-created BigQuery sessions (CALL BQ.ABORT_SESSION();whenEnableSession=1) even when the calling thread is interrupted while preserving caller-configuredQueryProperties=session_id=...properties, surfacesBigQueryExceptionon pooled session abort while preserving un-aborted transaction/session state for fallback physicalclose(), clearsclientInfoandsqlWarnings(backed byCopyOnWriteArrayListfor concurrent snapshot reads), and restoresholdability.abort(Executor)& MDC preservation: SerializesresetForReuse()andcloseImpl()vialifecycleLock; coordinatesabort(Executor)andclose()viacloseStateLocksoisClosedremainsfalseuntilexecutor.executeaccepts/starts while gatingcheckClosed()oncloseInitiated; honors configuredTimeoutand inspectsJobStatuson transaction commit/rollback job waits; and saves/restores outer thread MDCconnectionIdinBigQueryJdbcMdc.registerInstance,close(), andabort(Executor).BigQueryPooledConnection& dynamic handle/child proxies: ReplacesBigQueryPooledConnectionWrapperwithPooledConnectionInvocationHandlerbacked by a per-handleReentrantReadWriteLockshared with pooledStatementandDatabaseMetaDataproxies so operational calls hold the read lock during RPCs whileclose(), handle reclaim, and physical teardown await quiescence. Wraps childStatement/PreparedStatement/CallableStatementandDatabaseMetaDataobjects (and attaches pooled proxies onBigQueryStatementforResultSet.getStatement()) sogetConnection()enforcesparentHandler.checkClosed()and returns the logicalConnectionhandle. FiresconnectionErrorOccurredand physically closesBigQueryConnectionon reset failures or out-of-band physical closes, dispatchesabort(Executor)teardown asynchronously, isolates listenerRuntimeExceptions, and scopes MDC insidephysicalLock.PooledConnectionDataSource/PooledConnectionListener): Removes the cached sharedbqConnectionfield fromPooledConnectionDataSourceso cache misses open a dedicatedBigQueryConnection, recycles idle physical connections into freshBigQueryPooledConnectionholders while retiring prior holders (State.RETIRED), separates the internal pool listener from publicListenerPoolSizeslots with sticky external ownership onaddConnectionEventListener, uses atomic LIFO (addFirst/pollFirst) checkout underpoolLock, closes excess driver-owned idle connections atConnectionPoolSize(0= unbounded), and clarifiesConnectionPoolSizeandListenerPoolSizeindocs/USER_GUIDE.md.Behavior Changes
PooledConnectionDataSource.getPooledConnection()opens a dedicated physicalBigQueryConnection, and idle-pool hits wrap the recycled physical connection in a newPooledConnectionwhile retiring the previous holder.Connection.close()on a pooled handle synchronously runs state/transaction/session reset RPCs when needed and propagates any resetSQLExceptionafter firingconnectionErrorOccurred.BigQueryPooledConnection.close()physically closesBigQueryConnectionand removes the holder fromPooledConnectionListenerwithout firingconnectionClosed.BigQueryPooledConnection.getConnection()while a handle is open invalidates the prior handle and resets connection state without firingconnectionClosed(PooledConnection#getConnection(), JDBC 4.3 section 11.1).ConnectionPoolSizecloses the excess physical connection inline on the caller thread (ConnectionPoolSize=0retains all idle connections).Statement/DatabaseMetaDatathrowBigQueryJdbcException(SQLClientInfoExceptionwithClientInfoStatus.REASON_UNKNOWNforsetClientInfo).Testing
BigQueryConnectionTest,BigQueryJdbcMdcTest,BigQueryPooledConnectionTest,PooledConnectionDataSourceTest, andPooledConnectionListenerTest(1,384unit tests passing).ITConnectionPoolingTestverifying independent concurrent checkouts and end-to-end session and uncommitted-transaction reset on pool return.