Repository navigation
Reject database/sql batch preparation outside a transaction - #2144
Open
powerman2026 wants to merge 1 commit into
Open
powerman2026 wants to merge 1 commit into
powerman2026 wants to merge 1 commit into
Conversation
powerman2026
requested review from
a team,
kavirajk and
polyglotAI-bot
as code owners
November 18, 2026 17:40
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
Fixes #2127. Only a transaction commit sends a prepared batch. Previously
DB.PrepareContextcould start an INSERT on a pooled connection with no transaction to send the rows. With the native protocol, the next borrower could also receive a protocol error. Reject the prepare before opening the batch and direct the caller toTx.PrepareorTx.PrepareContext.The regression uses one pooled connection against a real ClickHouse 26.9.27.3 server on both protocols. Both accepted the invalid prepare before the change. After the change they reject it before and after a successful transaction, and the transaction's row can be read after closing the statement and committing. Updated the existing #741 cases to commit their inserts.
Verification
-racefor native and HTTP.go test -race -count=1 . ./lib/...passed.go vet ./...reports the same findings in unchanged files with the original driver implementation and with this change.Checklist
Note
Medium Risk
Breaking behavior for callers that used
db.Preparewithout a transaction; change touches connection pooling and batch lifecycle on the std driver path.Overview
database/sqlbatch prepares (Prepare/PrepareContext) are now rejected on a plain connection and must run inside a transaction viaTx.PrepareorTx.PrepareContext, because prepared batches are only sent onCommit(viabatch.Send). Allowing prepare outside a tx could leave an INSERT batch open on a pooled connection and break the next borrower (fixes #2127).The std driver tracks transaction state with
inTx, set inBeginTxand cleared onCommit/Rollback.Begin()now delegates toBeginTx. Existing issue #741 tests were updated to begin a tx, prepare, exec, and commit; a new regression test asserts the error outside a tx and success inside one on native and HTTP.Reviewed by Cursor Bugbot for commit 1652aa5. Bugbot is set up for automated code reviews on this repo. Configure here.