Skip to content

fix(bigquery-jdbc): enforce PooledConnection lifecycle and closed-handle guards - #15203

Merged
keshavdandeva merged 2 commits into
fix/jdbc-pooling-1-connection-resetfrom
fix/jdbc-pooling-2-pooled-connection-lifecycle
Nov 27, 2026
Merged

keshavdandeva merged 2 commits into
fix/jdbc-pooling-1-connection-resetfrom
fix/jdbc-pooling-2-pooled-connection-lifecycle

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 2 of 3 for JDBC connection pooling lifecycle and state-reset fixes (stacked on #15202).

Brings BigQueryPooledConnection and its logical Connection handles into compliance with JDBC 4.3 PooledConnection and Connection lifecycle contracts:

  • Connection state reset on logical return and handle reclaim: Invokes BigQueryConnection.resetForReuse() serialized under physicalLock when a logical Connection handle closes or when PooledConnection.getConnection() reclaims an open handle. If reset fails—or if the underlying physical connection was closed out-of-band—transitions the holder to CLOSED, physically closes BigQueryConnection, and fires connectionErrorOccurred so pool managers evict the broken slot.
  • Dynamic handle & child proxies with ReentrantReadWriteLock: Replaces the hand-written BigQueryPooledConnectionWrapper class with PooledConnectionInvocationHandler backed by a per-handle ReentrantReadWriteLock shared with pooled Statement and DatabaseMetaData proxies so operational calls across Connection, Statement, and DatabaseMetaData hold the read lock during RPCs while close(), reclaim, and physical teardown await quiescence before resetting or closing.
  • Child Statement, ResultSet.getStatement(), and DatabaseMetaData scoping: Wraps pooled Statement / PreparedStatement / CallableStatement and DatabaseMetaData objects (and attaches the Statement proxy on BigQueryStatement for ResultSet.getStatement()) so getConnection() enforces parentHandler.checkClosed() and returns the logical Connection handle, and retained DatabaseMetaData references reject operations after Connection.close() or Connection.abort(Executor).
  • Non-blocking abort(Executor), listener isolation & method-scoped MDC: Dispatches physical abort teardown onto the caller's Executor (including preempting an in-flight reset), isolates RuntimeExceptions thrown by ConnectionEventListener callbacks, and uses method-scoped MDC connectionId across pooled lifecycle entry points while preserving outer thread MDC context and omitting logs after physical close unmounts the per-connection file handler.

Behavior Changes

  • 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).
  • Operational calls on a closed logical handle or retained DatabaseMetaData throw BigQueryJdbcException (SQLClientInfoException with ClientInfoStatus.REASON_UNKNOWN for setClientInfo).
  • BigQueryPooledConnection.addConnectionEventListener throws BigQueryJdbcRuntimeException when ListenerPoolSize capacity is exceeded.

Testing

  • Added unit tests in BigQueryPooledConnectionTest covering logical-close reset, two-thread setAutoCommit and statement-creation races against close(), child Statement/DatabaseMetaData.getConnection() scoping, out-of-band physical close error events, listener exception isolation, async abort(Executor) (including blocked-reset preemption), physicalLock serialization, outer thread MDC preservation across pooled calls and listener callbacks, and post-close handle guards.
  • Updated physical-close assertions and closed leaked PooledConnection instances in PooledConnectionListenerTest and ITConnectionPoolingTest.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors BigQueryPooledConnection and its inner wrapper class to enhance thread safety, state management, and JDBC compliance. It introduces dedicated locks (physicalLock and stateLock) and implements robust cleanup and reset logic on logical close, abort, and connection errors. The review feedback highlights a potential race condition where closePhysicalQuietly is called without acquiring physicalLock, and suggests synchronizing this method to ensure thread safety, which also allows simplifying fireConnectionError.

@keshavdandeva
keshavdandeva force-pushed the fix/jdbc-pooling-2-pooled-connection-lifecycle branch from f7f425a to 545505b Compare November 24, 2026 22:46
@keshavdandeva
keshavdandeva force-pushed the fix/jdbc-pooling-2-pooled-connection-lifecycle branch from 545505b to e1159b0 Compare November 27, 2026 14:21

@Neenu1995 Neenu1995 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great — thanks for serializing resetForReuse()/closeImpl() via lifecycleLock and updating the BigQueryJdbcMdc scoping and getConnection() closed-handle checks!

…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
keshavdandeva force-pushed the fix/jdbc-pooling-2-pooled-connection-lifecycle branch from e1159b0 to 622760a Compare November 27, 2026 17:06
@keshavdandeva
keshavdandeva merged commit 504cb5a into main Nov 27, 2026
314 of 331 checks passed
@keshavdandeva
keshavdandeva deleted the fix/jdbc-pooling-2-pooled-connection-lifecycle branch November 27, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants