Skip to content

Reject database/sql batch preparation outside a transaction - #2144

Open
powerman2026 wants to merge 1 commit into
ClickHouse:mainfrom
powerman2026:fix/std-prepare-transaction
Open

powerman2026 wants to merge 1 commit into
ClickHouse:mainfrom
powerman2026:fix/std-prepare-transaction

Conversation

@powerman2026

@powerman2026 powerman2026 commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

Fixes #2127. Only a transaction commit sends a prepared batch. Previously DB.PrepareContext could 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 to Tx.Prepare or Tx.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

Checklist

  • Integration tests cover the transaction boundary and connection reuse.

Note

Medium Risk
Breaking behavior for callers that used db.Prepare without a transaction; change touches connection pooling and batch lifecycle on the std driver path.

Overview
database/sql batch prepares (Prepare / PrepareContext) are now rejected on a plain connection and must run inside a transaction via Tx.Prepare or Tx.PrepareContext, because prepared batches are only sent on Commit (via batch.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 in BeginTx and cleared on Commit / Rollback. Begin() now delegates to BeginTx. 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.

@cla-assistant

cla-assistant Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant