Skip to content

Use single global transaction per process - #798

Merged
staabm merged 9 commits into
mainfrom
less-trabs
Jul 13, 2026
Merged

Use single global transaction per process#798
staabm merged 9 commits into
mainfrom
less-trabs

Conversation

@staabm

@staabm staabm commented Jul 13, 2026

Copy link
Copy Markdown
Owner

closes #797

grafik

Copilot AI 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.

Pull request overview

This PR introduces a process-wide “global transaction” to avoid starting/rolling back a transaction for every analyzed query, reducing transaction-handling overhead during query reflection and related MySQL schema/query-plan operations.

Changes:

  • Add GlobalTransaction::ensureStarted() and switch query simulation/analyzers to reuse a single transaction.
  • Remove per-query beginTransaction()/rollBack() (PDO) and begin_transaction()/rollback() (mysqli) wrappers from reflectors/analyzers.
  • Start the global transaction for schema hashing and roll it back via a shutdown handler.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/QueryReflection/PdoPgSqlQueryReflector.php Uses GlobalTransaction::ensureStarted() instead of per-query transaction begin/rollback.
src/QueryReflection/PdoMysqlQueryReflector.php Uses GlobalTransaction::ensureStarted() instead of per-query transaction begin/rollback.
src/QueryReflection/MysqliQueryReflector.php Uses GlobalTransaction::ensureStarted() instead of per-query transaction begin/rollback.
src/QueryReflection/GlobalTransaction.php New helper to lazily start one transaction and roll it back on shutdown.
src/DbSchema/SchemaHasherMysql.php Starts the global transaction before schema hashing queries.
src/Analyzer/QueryPlanAnalyzerMysql.php Starts the global transaction before EXPLAIN query-plan analysis.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +19 to +41
if (self::$inTransaction) {
return;
}
self::$inTransaction = true;

try {
if (QueryReflection::getRuntimeConfiguration()->isAnalyzingWriteQueries()) {
if ($connection instanceof PDO) {
$connection->beginTransaction();
} else {
$connection->begin_transaction();
}
} else {
if ($connection instanceof PDO) {
$connection->beginTransaction();
} else {
$connection->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);
}
}
} catch (PDOException $e) {
// not all drivers may support transactions
throw new \RuntimeException('Failed to start transaction', $e->getCode(), $e);
}
Comment thread src/QueryReflection/GlobalTransaction.php Outdated
Comment thread src/QueryReflection/PdoPgSqlQueryReflector.php
Comment thread src/Analyzer/QueryPlanAnalyzerMysql.php
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@staabm
staabm marked this pull request as ready for review July 13, 2026 14:12
@staabm
staabm merged commit 9cfdd8c into main Jul 13, 2026
28 checks passed
@staabm
staabm deleted the less-trabs branch July 13, 2026 14:18
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.

use a single transaction for all db interactions

3 participants