Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the transaction close callback registration mechanism by replacing the temporary withTransactionCloseFunc with a chained registerTransactionCloseFunc on the active transaction. It also improves robustness by checking for canceled contexts early in beginTx and ensuring proper cleanup (rollback and connection closing) on transaction initialization errors to prevent connection leaks. Additionally, comprehensive tests are added to verify transaction lifecycles, canceled contexts, and late registration of close functions. Feedback was provided on a potential panic in the test helper cancelOnBeginContext.Err() due to non-thread-safe channel closing, suggesting the use of sync.Once.
Previously, custom transaction close callbacks (such as returning a checked-out connection to the pool when a transaction ends) were temporarily stored as a field on the connection struct before the transaction was initiated. This transient state risked lingering across connection pool reuses and required coordinating state across separate begin steps. In addition, if starting a transaction failed early, checked-out connections could occasionally leak. This change refactors transaction cleanup by registering close callbacks directly onto active transactions after they are created. Key improvements: - Scopes close callbacks strictly to the active transaction lifecycle, preventing stray cleanup state from leaking across pooled connections. - Fixes connection leaks on failed transaction starts and canceled contexts by ensuring connections are immediately returned to the pool on error. - Supports dynamically registered cleanup hooks so that callbacks added after a transaction begins executing are reliably invoked on commit or rollback. - Simplifies the transaction begin helpers across read-write, read-only, and batch read-only workflows.
6163e46 to
4748e50
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the transaction close callback registration mechanism in the Spanner driver by replacing the pre-transaction close function storage with a dynamic registration method (registerTransactionCloseFunc) that chains close callbacks directly on the active transaction. This change ensures robust cleanup and prevents connection leaks, particularly when transactions are initiated with canceled contexts or when setting transaction options fails. Additionally, comprehensive unit, mock server, and lifecycle tests have been added to validate the new registration behavior, error handling, and connection leak prevention. I have no feedback to provide as the changes are well-implemented and adhere to the repository's style guidelines.
Previously, custom transaction close callbacks (such as returning a checked-out connection to the pool when a transaction ends) were temporarily stored as a field on the connection struct before the transaction was initiated. This transient state risked lingering across connection pool reuses and required coordinating state across separate begin steps. In addition, if starting a transaction failed early, checked-out connections could occasionally leak.
This change refactors transaction cleanup by registering close callbacks directly onto active transactions after they are created.
Key improvements: