Repository navigation
fix(bigquery-jdbc): enforce PooledConnection lifecycle and closed-handle guards - #15203
Conversation
There was a problem hiding this comment.
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.
f7f425a to
545505b
Compare
545505b to
e1159b0
Compare
Neenu1995
left a comment
There was a problem hiding this comment.
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.
e1159b0 to
622760a
Compare
Summary
Part 2 of 3 for JDBC connection pooling lifecycle and state-reset fixes (stacked on #15202).
Brings
BigQueryPooledConnectionand its logicalConnectionhandles into compliance with JDBC 4.3PooledConnectionandConnectionlifecycle contracts:BigQueryConnection.resetForReuse()serialized underphysicalLockwhen a logicalConnectionhandle closes or whenPooledConnection.getConnection()reclaims an open handle. If reset fails—or if the underlying physical connection was closed out-of-band—transitions the holder toCLOSED, physically closesBigQueryConnection, and firesconnectionErrorOccurredso pool managers evict the broken slot.ReentrantReadWriteLock: Replaces the hand-writtenBigQueryPooledConnectionWrapperclass withPooledConnectionInvocationHandlerbacked by a per-handleReentrantReadWriteLockshared with pooledStatementandDatabaseMetaDataproxies so operational calls acrossConnection,Statement, andDatabaseMetaDatahold the read lock during RPCs whileclose(), reclaim, and physical teardown await quiescence before resetting or closing.Statement,ResultSet.getStatement(), andDatabaseMetaDatascoping: Wraps pooledStatement/PreparedStatement/CallableStatementandDatabaseMetaDataobjects (and attaches theStatementproxy onBigQueryStatementforResultSet.getStatement()) sogetConnection()enforcesparentHandler.checkClosed()and returns the logicalConnectionhandle, and retainedDatabaseMetaDatareferences reject operations afterConnection.close()orConnection.abort(Executor).abort(Executor), listener isolation & method-scoped MDC: Dispatches physical abort teardown onto the caller'sExecutor(including preempting an in-flight reset), isolatesRuntimeExceptions thrown byConnectionEventListenercallbacks, and uses method-scoped MDCconnectionIdacross 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 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).DatabaseMetaDatathrowBigQueryJdbcException(SQLClientInfoExceptionwithClientInfoStatus.REASON_UNKNOWNforsetClientInfo).BigQueryPooledConnection.addConnectionEventListenerthrowsBigQueryJdbcRuntimeExceptionwhenListenerPoolSizecapacity is exceeded.Testing
BigQueryPooledConnectionTestcovering logical-close reset, two-threadsetAutoCommitand statement-creation races againstclose(), childStatement/DatabaseMetaData.getConnection()scoping, out-of-band physical close error events, listener exception isolation, asyncabort(Executor)(including blocked-reset preemption),physicalLockserialization, outer thread MDC preservation across pooled calls and listener callbacks, and post-close handle guards.PooledConnectioninstances inPooledConnectionListenerTestandITConnectionPoolingTest.